Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -19,9 +19,9 @@ def generate_text(prompt, max_length, temperature):
|
|
19 |
{"role": "user", "content": prompt}
|
20 |
]
|
21 |
formatted_prompt = tokenizer.apply_chat_template(messages, add_generation_prompt=True, tokenize=False)
|
22 |
-
|
23 |
inputs = tokenizer(formatted_prompt, return_tensors="pt").to(model.device)
|
24 |
-
|
25 |
outputs = model.generate(
|
26 |
**inputs,
|
27 |
max_new_tokens=max_length,
|
@@ -30,9 +30,10 @@ def generate_text(prompt, max_length, temperature):
|
|
30 |
top_k=100,
|
31 |
top_p=0.95,
|
32 |
)
|
33 |
-
|
34 |
return tokenizer.decode(outputs[0][inputs['input_ids'].shape[1]:], skip_special_tokens=True)
|
35 |
|
|
|
36 |
# Custom CSS
|
37 |
css = """
|
38 |
body {
|
@@ -127,6 +128,7 @@ example_prompts = [
|
|
127 |
"Describe the process of photosynthesis in simple terms."
|
128 |
]
|
129 |
|
|
|
130 |
# Gradio interface
|
131 |
with gr.Blocks(css=css) as iface:
|
132 |
gr.HTML(
|
@@ -138,19 +140,12 @@ with gr.Blocks(css=css) as iface:
|
|
138 |
</div>
|
139 |
"""
|
140 |
)
|
141 |
-
|
142 |
with gr.Group():
|
143 |
-
gr.
|
144 |
-
""
|
145 |
-
|
146 |
-
|
147 |
-
<ul>
|
148 |
-
""" + "".join([f"<li>{prompt}</li>" for prompt in example_prompts]) + """
|
149 |
-
</ul>
|
150 |
-
</div>
|
151 |
-
"""
|
152 |
-
)
|
153 |
-
|
154 |
with gr.Group(elem_classes="input-group"):
|
155 |
prompt = gr.Textbox(label="Prompt", placeholder="Enter your prompt here...", lines=5)
|
156 |
max_length = gr.Slider(minimum=1, maximum=500, value=128, step=1, label="Max Length")
|
@@ -162,20 +157,9 @@ with gr.Blocks(css=css) as iface:
|
|
162 |
|
163 |
generate_btn.click(generate_text, inputs=[prompt, max_length, temperature], outputs=output)
|
164 |
|
165 |
-
#
|
166 |
-
|
167 |
-
|
168 |
-
<script>
|
169 |
-
document.addEventListener('DOMContentLoaded', (event) => {
|
170 |
-
document.querySelectorAll('.example-prompts li').forEach(item => {
|
171 |
-
item.addEventListener('click', event => {
|
172 |
-
document.querySelector('textarea[data-testid="textbox"]').value = event.target.textContent;
|
173 |
-
});
|
174 |
-
});
|
175 |
-
});
|
176 |
-
</script>
|
177 |
-
"""
|
178 |
-
)
|
179 |
|
180 |
# Launch the app
|
181 |
iface.launch()
|
|
|
19 |
{"role": "user", "content": prompt}
|
20 |
]
|
21 |
formatted_prompt = tokenizer.apply_chat_template(messages, add_generation_prompt=True, tokenize=False)
|
22 |
+
|
23 |
inputs = tokenizer(formatted_prompt, return_tensors="pt").to(model.device)
|
24 |
+
|
25 |
outputs = model.generate(
|
26 |
**inputs,
|
27 |
max_new_tokens=max_length,
|
|
|
30 |
top_k=100,
|
31 |
top_p=0.95,
|
32 |
)
|
33 |
+
|
34 |
return tokenizer.decode(outputs[0][inputs['input_ids'].shape[1]:], skip_special_tokens=True)
|
35 |
|
36 |
+
|
37 |
# Custom CSS
|
38 |
css = """
|
39 |
body {
|
|
|
128 |
"Describe the process of photosynthesis in simple terms."
|
129 |
]
|
130 |
|
131 |
+
# Gradio interface
|
132 |
# Gradio interface
|
133 |
with gr.Blocks(css=css) as iface:
|
134 |
gr.HTML(
|
|
|
140 |
</div>
|
141 |
"""
|
142 |
)
|
143 |
+
|
144 |
with gr.Group():
|
145 |
+
with gr.Group(elem_classes="example-prompts"):
|
146 |
+
gr.HTML("<h3>Example Prompts:</h3>")
|
147 |
+
example_buttons = [gr.Button(prompt) for prompt in example_prompts]
|
148 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
149 |
with gr.Group(elem_classes="input-group"):
|
150 |
prompt = gr.Textbox(label="Prompt", placeholder="Enter your prompt here...", lines=5)
|
151 |
max_length = gr.Slider(minimum=1, maximum=500, value=128, step=1, label="Max Length")
|
|
|
157 |
|
158 |
generate_btn.click(generate_text, inputs=[prompt, max_length, temperature], outputs=output)
|
159 |
|
160 |
+
# Set up example prompt buttons
|
161 |
+
for button in example_buttons:
|
162 |
+
button.click(lambda x: x, inputs=[button], outputs=[prompt])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
163 |
|
164 |
# Launch the app
|
165 |
iface.launch()
|