Spaces:
Runtime error
Runtime error
import gradio as gr | |
import os | |
import json | |
import requests | |
import openai | |
import re | |
#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) | |
regexed_prompt_string = re.sub(r'@\w+', '', prompt_string) | |
prompt_list = [element.split(". ")[1] for element in regexed_prompt_string.split(", ")] | |
prompt_list_prepend = [f"an photo of {item}, photorealistic, coherent, high quality, rendered in unreal engine" 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)) | |
sd_output = [] | |
for image_dir in image_dirs: | |
found_first_image = False | |
for filename in os.listdir(image_dir): | |
if filename.endswith('.json'): | |
continue | |
elif not found_first_image: | |
sd_output.append(os.path.join(image_dir, filename)) | |
found_first_image = True | |
return sd_output, prompt_string | |
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 🍵") | |
gr.Markdown("This application uses Stable Diffusion space, so if you get `The queue is full!` please try again later, give the artist some break 🧑🎨") | |
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") | |
openai_output = gr.Textbox(lines=4) | |
enter_username.click(get_images, | |
inputs = [ | |
input_text | |
], | |
outputs = [sd_output, openai_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: | |
twitter_stream = re.sub(r'@\w+', '', 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. | |
Ignore any mentions to other people. | |
Examples: | |
Twitter stream: | |
noughties Turkey was the coolest | |
listening to this set for flow today 🏞️ | |
this meme isn't brought to you by my hugging face persona, that one is below | |
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. | |
today we eat börek & kurabiye | |
I feel like I will never ever love anything like I love mediterrenean 🌴🌊💙 I don't know if anyone else feels this way | |
I'm ranked 10th on github trending today 😭 https://github.com/trending/developers | |
paris is never cold, people are underdressed | |
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. meditations | |
Twitter stream: | |
Well im suck again, but imma stream some resident evil 4 if you wanna chill | |
I thought wearing a turtle neck and gold chain was a meme | |
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. | |
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! | |
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. | |
Every video I make | |
THERE IS TOO MUCH TO DO IN ELDEN RING | |
Overall my move to Australia has been pretty great. | |
Went out for a Karaoke night with some friends but when it was my turn to sing. | |
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: | |
Meet Sonny! Our brand new coffee mascot! He’s brewing up some fun. | |
This World Cup final is insane!!! | |
I'm going to be on The Late Late Show in Ireland again this Friday! | |
My next video will be my 5,000th upload | |
CAT GAME 🐈 https://youtube.com/watch?v=OaE_UPIfiAo | |
Happy Valentine’s Day ❤️ I hope you feel loved today, even if that love has to come from yourself. Treat yourself. Evelien surprised me 😊 | |
Starting a Let’s Play channel with Tom Holland | |
I normally never do reviews but I couldn’t NOT talk about Dune! https://youtu.be/yh9Evgt6ZBg | |
@Spider-Man 2 Can’t wait!! | |
Concepts: 1. coffee 2. football 3. Ireland 4. video 5. cat 6. love 7. gaming 8. Dune 9. spider-man | |
Now, provide the list of 9 concepts for following twitter stream: | |
Twitter stream: {twitter_stream} | |
Concepts: | |
""" | |
result = openai_create(prompt=prompt_text) | |
print(result) | |
return result | |
####### | |
demo.queue().launch(debug=True) |