Skip to content

Commit

Permalink
added the huggingface code and updated reddit integration with autogen
Browse files Browse the repository at this point in the history
  • Loading branch information
tylerprogramming committed Jun 6, 2024
1 parent 5015af5 commit 27cdabd
Show file tree
Hide file tree
Showing 4 changed files with 157 additions and 25 deletions.
73 changes: 73 additions & 0 deletions _integrations/reddit/main.py
Original file line number Diff line number Diff line change
@@ -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)









72 changes: 47 additions & 25 deletions _integrations/reddit/reddit.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -30,7 +30,7 @@

user_proxy = autogen.UserProxyAgent(
name="Admin",
human_input_mode="ALWAYS",
human_input_mode="NEVER",
code_execution_config=False,
)

Expand All @@ -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,
)
Expand All @@ -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"]
Expand All @@ -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
17 changes: 17 additions & 0 deletions huggingface/llama.py
Original file line number Diff line number Diff line change
@@ -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)
20 changes: 20 additions & 0 deletions huggingface/main.py
Original file line number Diff line number Diff line change
@@ -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")

0 comments on commit 27cdabd

Please sign in to comment.