Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,47 +1,73 @@
|
|
1 |
import os
|
2 |
-
import io
|
3 |
import json
|
4 |
import random
|
|
|
5 |
import requests
|
6 |
import gradio as gr
|
7 |
|
8 |
-
class TextGenerator:
|
9 |
-
def __init__(self, token, model_id):
|
10 |
-
self.token = token
|
11 |
-
self.model_id = model_id
|
12 |
-
|
13 |
-
def generate_text(self, prompt, hf_model=None, hf_token=None, parameters=None):
|
14 |
-
if hf_token is None:
|
15 |
-
hf_token = self.token
|
16 |
-
if hf_model is None:
|
17 |
-
hf_model = self.model_id
|
18 |
-
if parameters is None:
|
19 |
-
parameters = {'max_new_tokens': 512, 'do_sample': True, 'return_full_text': False, 'temperature': 1.0, 'top_k': 50, 'repetition_penalty': 1.2}
|
20 |
-
url = f'https://api-inference.huggingface.co/models/{hf_model}'
|
21 |
-
headers = {'Authorization': f'Bearer {hf_token}', 'Content-type': 'application/json'}
|
22 |
-
data = {'inputs': prompt, 'stream': False, 'options': {'use_cache': False}, 'parameters': parameters}
|
23 |
-
r = requests.post(url, headers=headers, data=json.dumps(data))
|
24 |
-
return json.loads(r.content.decode("utf-8"))[0]['generated_text']
|
25 |
-
|
26 |
class GradioUI:
|
27 |
-
def __init__(self
|
28 |
-
self.
|
29 |
-
|
30 |
-
|
|
|
|
|
31 |
|
32 |
def random_title(self):
|
33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
|
35 |
def generate_interface(self):
|
36 |
with gr.Blocks(css=self.styles) as demo:
|
37 |
-
|
|
|
38 |
random_title_btn.click(fn=None, inputs=None, outputs=[title], _js=f"return ['{self.random_title()}'];")
|
39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
40 |
demo.queue(concurrency_count=5, max_size=256)
|
41 |
demo.launch()
|
42 |
|
43 |
-
|
44 |
-
model_id = 'meta-llama/Llama-2-70b-chat-hf'
|
45 |
-
text_gen = TextGenerator(token, model_id)
|
46 |
-
ui = GradioUI(text_gen)
|
47 |
ui.generate_interface()
|
|
|
1 |
import os
|
|
|
2 |
import json
|
3 |
import random
|
4 |
+
import string
|
5 |
import requests
|
6 |
import gradio as gr
|
7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
class GradioUI:
|
9 |
+
def __init__(self):
|
10 |
+
self.styles = """
|
11 |
+
... (styles omitted for brevity) ...
|
12 |
+
"""
|
13 |
+
self.token = os.getenv('HF_TOKEN')
|
14 |
+
self.model_id = 'meta-llama/Llama-2-70b-chat-hf'
|
15 |
|
16 |
def random_title(self):
|
17 |
+
titles = [
|
18 |
+
'Pokemon training story',
|
19 |
+
'The Sun of Shangri-La',
|
20 |
+
'Man In The Future',
|
21 |
+
'Friends',
|
22 |
+
'Cyborg Of A Beast',
|
23 |
+
'Man At The Graveyard',
|
24 |
+
'Vampire Of The Land',
|
25 |
+
'A software engineer who is looking for job',
|
26 |
+
'A software engineer licensed to kill'
|
27 |
+
]
|
28 |
+
return random.choice(titles)
|
29 |
+
|
30 |
+
def generate_text(self, prompt):
|
31 |
+
url = f'https://api-inference.huggingface.co/models/{self.model_id}'
|
32 |
+
headers = {
|
33 |
+
'Authorization': f'Bearer {self.token}',
|
34 |
+
'Content-type': 'application/json'
|
35 |
+
}
|
36 |
+
data = {
|
37 |
+
'inputs': prompt,
|
38 |
+
'stream': False,
|
39 |
+
'options': {
|
40 |
+
'use_cache': False,
|
41 |
+
},
|
42 |
+
'parameters': {
|
43 |
+
'max_new_tokens': 512,
|
44 |
+
'do_sample': True,
|
45 |
+
'return_full_text': False,
|
46 |
+
'temperature': 1.0,
|
47 |
+
'top_k': 50,
|
48 |
+
'repetition_penalty': 1.2
|
49 |
+
}
|
50 |
+
}
|
51 |
+
|
52 |
+
r = requests.post(url, headers=headers, data=json.dumps(data))
|
53 |
+
if r.reason != 'OK':
|
54 |
+
raise ValueError("Response other than 200")
|
55 |
+
return json.loads(r.content.decode("utf-8"))[0]['generated_text']
|
56 |
|
57 |
def generate_interface(self):
|
58 |
with gr.Blocks(css=self.styles) as demo:
|
59 |
+
title = gr.Textbox(placeholder="Title", value=self.random_title())
|
60 |
+
random_title_btn = gr.Button("Get Random Title")
|
61 |
random_title_btn.click(fn=None, inputs=None, outputs=[title], _js=f"return ['{self.random_title()}'];")
|
62 |
+
editor = gr.Textbox(placeholder="Write your story here.", lines=32, max_lines=32, elem_classes=['no-label', 'small-big-textarea'])
|
63 |
+
gen_btn = gr.Button("Generate Text")
|
64 |
+
chatbot = gr.Chatbot([])
|
65 |
+
chat_txt = gr.Textbox(placeholder="Chat here", elem_classes=['no-label'])
|
66 |
+
|
67 |
+
# More Gradio elements and logic can be added here.
|
68 |
+
|
69 |
demo.queue(concurrency_count=5, max_size=256)
|
70 |
demo.launch()
|
71 |
|
72 |
+
ui = GradioUI()
|
|
|
|
|
|
|
73 |
ui.generate_interface()
|