From 27cdabda8da7c39730ca019d973e4d54c28fd943 Mon Sep 17 00:00:00 2001 From: tylerreed Date: Thu, 6 Jun 2024 16:32:25 -0400 Subject: [PATCH] added the huggingface code and updated reddit integration with autogen --- _integrations/reddit/main.py | 73 ++++++++++++++++++++++++++++++++++ _integrations/reddit/reddit.py | 72 +++++++++++++++++++++------------ huggingface/llama.py | 17 ++++++++ huggingface/main.py | 20 ++++++++++ 4 files changed, 157 insertions(+), 25 deletions(-) create mode 100644 _integrations/reddit/main.py create mode 100644 huggingface/llama.py create mode 100644 huggingface/main.py diff --git a/_integrations/reddit/main.py b/_integrations/reddit/main.py new file mode 100644 index 0000000..4eee99e --- /dev/null +++ b/_integrations/reddit/main.py @@ -0,0 +1,73 @@ +import streamlit as st +import json +import reddit + +st.title = "Reddit App" + +with st.container(border=True): + mode = st.radio( + "Choose a mode", + [":rainbow[Random]", "***Specific Subreddit***"], + captions=["Laugh out loud.", "Get the popcorn."] + ) + + number = st.slider("Pick a number", 1, 10) + + response = "" + + if mode == ":rainbow[Random]": + prompt = f"Get me {number} random subreddit(s)!" + submit = st.button(prompt) + + if submit: + response = reddit.start_chat(prompt) + + if mode == "***Specific Subreddit***": + input = st.text_input("") + submit = st.button(f"Give me content!") + prompt = f"Give me {number} subreddit posts on: {input}" + + if submit: + response = reddit.start_chat(prompt) + +with st.spinner('Wait for it...'): + if response: + st.success('Done!') + response = json.loads(response) + st.divider() + + for data in response: + # Extracting 'content' + content = data["content"] + + # Extracting 'meta_data' + meta_data = data["meta_data"] + + # Extracting each property from 'meta_data' + post_subreddit = meta_data["subreddit"] + post_category = meta_data["category"] + post_title = meta_data["title"] + post_score = meta_data["score"] + post_url = meta_data["url"] + post_author = meta_data["author"] + + st.header("Metadata") + st.divider() + st.markdown('**Subreddit**: ' + post_subreddit) + st.markdown('**Category**: ' + post_category) + st.markdown('**Title**: ' + post_title) + st.markdown('**URL**: ' + post_url) + st.markdown('**Author**: ' + post_author) + + st.header("Content") + st.divider() + st.write(content) + + + + + + + + + diff --git a/_integrations/reddit/reddit.py b/_integrations/reddit/reddit.py index fcecd73..b70edb5 100644 --- a/_integrations/reddit/reddit.py +++ b/_integrations/reddit/reddit.py @@ -5,7 +5,7 @@ from dotenv import load_dotenv from typing_extensions import Annotated -from _integrations.reddit.reddit_classes import RedditPostsLoader +from reddit_classes import RedditPostsLoader load_dotenv() @@ -30,7 +30,7 @@ user_proxy = autogen.UserProxyAgent( name="Admin", - human_input_mode="ALWAYS", + human_input_mode="NEVER", code_execution_config=False, ) @@ -45,7 +45,7 @@ def get_random_subreddit( client_id=os.getenv("CLIENT_ID"), client_secret=os.getenv("CLIENT_SECRET"), user_agent="extractor by u/tyler_programming", - categories=["new", "hot"], + categories=["hot"], mode=mode, number_posts=number_of_posts, ) @@ -58,7 +58,7 @@ def get_random_subreddit( @user_proxy.register_for_execution() -@engineer.register_for_llm(description="Get a random subreddit") +@engineer.register_for_llm(description="Get a specified subreddit") def get_specified_subreddit( search_queries: Annotated[str, "The search queries comma separated"], number_of_posts: Annotated[int, "The number of posts to retrieve, default should be 1"] @@ -82,24 +82,46 @@ def get_specified_subreddit( return serialized_data -chat_result = user_proxy.initiate_chat( - engineer, - message=""" - I need you to integrate with Reddit. Whenever you retrieve a post, you will get a RedditReader Object list and - I would like you to format the content and meta_data contained within. - - Metadata is in this format for each item in the list: - - reddit_list.post_subreddit, - reddit_list.post_category, - reddit_list.post_title, - reddit_list.post_score, - reddit_list.post_id, - reddit_list.post_url, - reddit_list.post_author - - Then also add the content from the item. - - Do not start until I give a command. -""", -) +def start_chat(input: str): + chat_result = user_proxy.initiate_chat( + engineer, + max_turns=2, + message=f""" + I need you to integrate with Reddit. Whenever you retrieve a post, you will get a RedditReader Object list and + I would like you to format the content and meta_data contained within. + + Metadata is in this format for each item in the list: + + reddit_list.post_subreddit, + reddit_list.post_category, + reddit_list.post_title, + reddit_list.post_score, + reddit_list.post_id, + reddit_list.post_url, + reddit_list.post_author + + Then also add the content from the item. + + Return this in proper json format, do not use triple tick marks to denote that it's json. If there is more + than 1 object, make sure it's an array. + + [ + {{ + "meta_data": {{ + "subreddit": the subreddit, + "category": the category, + "title": the title, + "score": the score, + "post_id": the post id, + "url": the url, + "author": the author + }}, + "content": the content + }} + ] + + {input} + """, + ) + + return chat_result.summary diff --git a/huggingface/llama.py b/huggingface/llama.py new file mode 100644 index 0000000..364336b --- /dev/null +++ b/huggingface/llama.py @@ -0,0 +1,17 @@ +import requests +import pprint as p + +API_URL = "https://api-inference.huggingface.co/models/meta-llama/Meta-Llama-3-8B-Instruct" +headers = {"Authorization": "Bearer hf_xxxx"} + + +def query(payload): + response = requests.post(API_URL, headers=headers, json=payload) + return response.json() + + +output = query({ + "inputs": "Can you give me a sample python function for binary search.", +}) + +p.pprint(output) diff --git a/huggingface/main.py b/huggingface/main.py new file mode 100644 index 0000000..70b950f --- /dev/null +++ b/huggingface/main.py @@ -0,0 +1,20 @@ +import requests + +API_URL = "https://api-inference.huggingface.co/models/stabilityai/stable-diffusion-xl-base-1.0" +headers = {"Authorization": "Bearer hf_xxxx"} + + +def query(payload): + response = requests.post(API_URL, headers=headers, json=payload) + return response.content + + +image_bytes = query({ + "inputs": "Astronaut riding a horse", +}) +# You can access the image with PIL.Image for example +import io +from PIL import Image + +image = Image.open(io.BytesIO(image_bytes)) +image.save("tester.png")