Spaces:
Runtime error
Runtime error
import gradio as gr | |
import os | |
import json | |
import requests | |
import openai | |
#Streaming endpoint | |
API_URL = "https://api.openai.com/v1/chat/completions" | |
TWITTER_BEARER = os.getenv("TWITTER_BEARER") | |
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY") | |
##### stable diffusion | |
stable_diffusion = gr.Blocks.load(name="spaces/runwayml/stable-diffusion-v1-5") | |
def get_images(username): | |
prompt_string = query(username) | |
prompt_list = [element.split(". ")[1] for element in prompt_string.split(", ")] | |
prompt_list_prepend = [f"a photo of {item}, hyperrealistic" for item in prompt_list] | |
print(prompt_list_prepend) | |
image_dirs = [] | |
for prompt in prompt_list_prepend: | |
image_dirs.append(stable_diffusion(prompt, fn_index=2)) | |
# do not take captions, and only take first output | |
sd_output = [] | |
for image_dir in image_dirs: | |
for filename in os.listdir(image_dir): | |
if filename.endswith('.jpg'): | |
sd_output.append(os.path.join(image_dir, filename)) | |
break | |
return sd_output | |
with gr.Blocks(theme="gradio/soft") as demo: | |
gr.Markdown("# π Starter Pack Generator π") | |
gr.Markdown("Enter a twitter username and this will return a starter pack for a person based on their latest tweets π¦") | |
gr.Markdown("Each generation might take up to a minute, please be patient, grab a green tea π΅") | |
with gr.Row(): | |
with gr.Column(): | |
input_text = gr.Textbox(label="Input user ID", | |
lines=4, elem_id="input-text") | |
with gr.Row(): | |
enter_username = gr.Button("Submit") | |
with gr.Column(elem_id="generated-gallery"): | |
sd_output = gr.Gallery().style(grid=3, height="auto") | |
enter_username.click(get_images, | |
inputs = [ | |
input_text | |
], | |
outputs = [sd_output] | |
) | |
######### | |
#### prompt creation | |
def openai_create(prompt): | |
openai.api_key=OPENAI_API_KEY | |
response = openai.Completion.create( | |
model="text-davinci-003", | |
max_tokens = 100, | |
prompt=prompt, stop="10.") | |
return response.choices[0].text | |
def query(username): | |
bearer_token = TWITTER_BEARER | |
headers = {"Authorization": "Bearer {}".format(bearer_token)} | |
url = f"https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name={username}&count={40}" | |
try: | |
response = requests.request("GET", url, headers = headers) | |
print(response) | |
twitter_stream = "" | |
for tweet in response.json(): | |
twitter_stream += tweet["text"] | |
except Exception as e: | |
print(e) | |
result = ["Please make sure you are providing a correct, public twitter account"] | |
if twitter_stream: | |
prompt_text=f"""You will be given a long twitter stream belonging to a specific profile. | |
I would like you to come up with a list of nine important concepts or items that user likes or is about them, and can be illustrated. | |
Examples: | |
Twitter stream: | |
@mervenoyann noughties Turkey was the coolest | |
@mervenoyann: listening to this set for flow today ποΈ | |
@mervenoyann: this meme isn't brought to you by my hugging face persona, that one is below | |
@mervenoyann: I often feel like what people really want vs what they say they want often mismatch. popular examples of this behavior these days, they want alexa to speak like generative models but they'd hate that if it happened. | |
@mervenoyann: today we eat bΓΆrek & kurabiye | |
@mervenoyann: I feel like I will never ever love anything like I love mediterrenean π΄ππ I don't know if anyone else feels this way | |
@mervenoyann: I'm ranked 10th on github trending today π https://github.com/trending/developers | |
@mervenoyann: paris is never cold, people are underdressed | |
@mervenoyann: my all time favorite books are Meditations by Marcus Aurelius & Discourses by Epictetus, makes your skin thicker and you more resilient to challenges of life | |
Concepts: 1. Turkish flag, 2. DJ set, 3. hugging face, 4. robot, 5. pastry, 6. mediterrenean, 7. github, 8. baguette bread, 9. marcus aurelius | |
Twitter stream: | |
@cinnamontoastken: Well im suck again, but imma stream some resident evil 4 if you wanna chill | |
@cinnamontoastken: I thought wearing a turtle neck and gold chain was a meme | |
@cinnamontoastken: Soon as I became old enough to wear "dad clothes" I dove right in. No hesitation. I've never been more comfortable in my life. | |
@cinnamontoastken: All the video and audio goes from gaming pc over the network to the stream pc. Skips all that annoying audio wiring. No problems yet! | |
@cinnamontoastken: The Silent Hill announcements are cool and all but the only things I've been able to rely on Konami to do in the past 10 years is aggressively claim music used in their videogames on YouTube, make pachinko machines, and cancel Silent Hill games. | |
@cinnamontoastken: Every video I make | |
@cinnamontoastken: THERE IS TOO MUCH TO DO IN ELDEN RING | |
@cinnamontoastken: Overall my move to Australia has been pretty great. | |
@cinnamontoastken: Went out for a Karaoke night with some friends but when it was my turn to sing all that came out was Chris Pratts voice. | |
Concepts: 1. resident evil zombies, 2. a man wearing a turtle neck and gold chain, 3. dad clothes, 4. pc setup, 5. silent hill game, 6. youtube, 7. elden ring, 8. australia, 9. karaoke set | |
Twitter stream: {twitter_stream} | |
Concepts: | |
""" | |
result = openai_create(prompt=prompt_text) | |
print(result) | |
return result | |
####### | |
#gr.Interface(query, inputs="text", outputs=gr.Textbox(lines=10), theme="gstaff/xkcd").launch(debug=True) | |
demo.launch(debug=True) |