merve HF staff commited on
Commit
2c872c4
β€’
1 Parent(s): 633d85d

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +142 -0
app.py ADDED
@@ -0,0 +1,142 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import os
3
+ import json
4
+ import requests
5
+ import openai
6
+
7
+ #Streaming endpoint
8
+ API_URL = "https://api.openai.com/v1/chat/completions"
9
+ TWITTER_BEARER = os.getenv("TWITTER_BEARER")
10
+
11
+ OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
12
+
13
+ ##### stable diffusion
14
+ stable_diffusion = gr.Blocks.load(name="spaces/runwayml/stable-diffusion-v1-5")
15
+
16
+
17
+
18
+ def get_images(username):
19
+ prompt_string = query(username)
20
+ prompt_list = [element.split(". ")[1] for element in prompt_string.split(", ")]
21
+ prompt_list_prepend = [f"a photo of {item}, hyperrealistic" for item in prompt_list]
22
+ print(prompt_list_prepend)
23
+
24
+ image_dirs = []
25
+ for prompt in prompt_list_prepend:
26
+ image_dirs.append(stable_diffusion(prompt, fn_index=2))
27
+
28
+ # do not take captions, and only take first output
29
+ sd_output = []
30
+ for image_dir in image_dirs:
31
+ for filename in os.listdir(image_dir):
32
+ if filename.endswith('.jpg'):
33
+ sd_output.append(os.path.join(image_dir, filename))
34
+ break
35
+
36
+ return sd_output
37
+
38
+
39
+
40
+ with gr.Blocks(theme="gradio/soft") as demo:
41
+ gr.Markdown("# 🎁 Starter Pack Generator 🎁")
42
+ gr.Markdown("Enter a twitter username and this will return a starter pack for a person based on their latest tweets 🦜")
43
+ gr.Markdown("Each generation might take up to a minute, please be patient, grab a green tea 🍡")
44
+
45
+ with gr.Row():
46
+ with gr.Column():
47
+ input_text = gr.Textbox(label="Input user ID",
48
+ lines=4, elem_id="input-text")
49
+ with gr.Row():
50
+ enter_username = gr.Button("Submit")
51
+
52
+
53
+ with gr.Column(elem_id="generated-gallery"):
54
+ sd_output = gr.Gallery().style(grid=3, height="auto")
55
+
56
+
57
+ enter_username.click(get_images,
58
+ inputs = [
59
+ input_text
60
+ ],
61
+ outputs = [sd_output]
62
+ )
63
+
64
+
65
+ #########
66
+ #### prompt creation
67
+
68
+
69
+ def openai_create(prompt):
70
+ openai.api_key=OPENAI_API_KEY
71
+ response = openai.Completion.create(
72
+ model="text-davinci-003",
73
+ max_tokens = 100,
74
+ prompt=prompt, stop="10.")
75
+ return response.choices[0].text
76
+
77
+
78
+
79
+ def query(username):
80
+
81
+ bearer_token = TWITTER_BEARER
82
+
83
+ headers = {"Authorization": "Bearer {}".format(bearer_token)}
84
+
85
+ url = f"https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name={username}&count={40}"
86
+ try:
87
+
88
+ response = requests.request("GET", url, headers = headers)
89
+ print(response)
90
+ twitter_stream = ""
91
+ for tweet in response.json():
92
+ twitter_stream += tweet["text"]
93
+ except Exception as e:
94
+ print(e)
95
+ result = ["Please make sure you are providing a correct, public twitter account"]
96
+ if twitter_stream:
97
+ prompt_text=f"""You will be given a long twitter stream belonging to a specific profile.
98
+ 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.
99
+ Examples:
100
+
101
+ Twitter stream:
102
+ @mervenoyann noughties Turkey was the coolest
103
+ @mervenoyann: listening to this set for flow today 🏞️
104
+ @mervenoyann: this meme isn't brought to you by my hugging face persona, that one is below
105
+ @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.
106
+ @mervenoyann: today we eat bΓΆrek & kurabiye
107
+ @mervenoyann: I feel like I will never ever love anything like I love mediterrenean πŸŒ΄πŸŒŠπŸ’™ I don't know if anyone else feels this way
108
+ @mervenoyann: I'm ranked 10th on github trending today 😭 https://github.com/trending/developers
109
+ @mervenoyann: paris is never cold, people are underdressed
110
+ @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
111
+
112
+ Concepts: 1. Turkish flag, 2. DJ set, 3. hugging face, 4. robot, 5. pastry, 6. mediterrenean, 7. github, 8. baguette bread, 9. marcus aurelius
113
+
114
+ Twitter stream:
115
+ @cinnamontoastken: Well im suck again, but imma stream some resident evil 4 if you wanna chill
116
+ @cinnamontoastken: I thought wearing a turtle neck and gold chain was a meme
117
+ @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.
118
+ @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!
119
+ @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.
120
+ @cinnamontoastken: Every video I make
121
+ @cinnamontoastken: THERE IS TOO MUCH TO DO IN ELDEN RING
122
+ @cinnamontoastken: Overall my move to Australia has been pretty great.
123
+ @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.
124
+
125
+ 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
126
+
127
+ Twitter stream: {twitter_stream}
128
+ Concepts:
129
+
130
+ """
131
+ result = openai_create(prompt=prompt_text)
132
+ print(result)
133
+
134
+
135
+ return result
136
+
137
+ #######
138
+
139
+
140
+ #gr.Interface(query, inputs="text", outputs=gr.Textbox(lines=10), theme="gstaff/xkcd").launch(debug=True)
141
+
142
+ demo.launch(debug=True)