Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,43 +1,36 @@
|
|
1 |
import gradio as gr
|
2 |
-
import torch
|
3 |
from transformers import pipeline
|
4 |
|
5 |
-
#
|
6 |
-
|
7 |
|
8 |
-
#
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
device_map="auto",
|
14 |
-
)
|
15 |
-
|
16 |
-
def generate_response(prompt, max_length=512, temperature=0.7):
|
17 |
-
# Format prompt for Llama 3 instruct style
|
18 |
-
formatted_prompt = f"<s>[INST] {prompt} [/INST]"
|
19 |
-
output = generator(
|
20 |
-
formatted_prompt,
|
21 |
max_length=max_length,
|
22 |
temperature=temperature,
|
23 |
-
|
24 |
-
top_p=0.95,
|
25 |
num_return_sequences=1,
|
|
|
26 |
)
|
27 |
-
|
28 |
-
|
29 |
-
response = generated_text.split("[/INST]")[-1].strip()
|
30 |
-
return response
|
31 |
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
|
|
|
|
|
|
|
|
41 |
|
42 |
-
|
43 |
-
|
|
|
1 |
import gradio as gr
|
|
|
2 |
from transformers import pipeline
|
3 |
|
4 |
+
# Load the TinyLlama model for text generation
|
5 |
+
pipe = pipeline("text-generation", model="TinyLlama/TinyLlama_v1.1")
|
6 |
|
7 |
+
# Define the prediction function
|
8 |
+
def generate_text(prompt, max_length=128, temperature=1.0, top_p=0.95):
|
9 |
+
# You can expose more parameters as needed
|
10 |
+
result = pipe(
|
11 |
+
prompt,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
max_length=max_length,
|
13 |
temperature=temperature,
|
14 |
+
top_p=top_p,
|
|
|
15 |
num_return_sequences=1,
|
16 |
+
do_sample=True
|
17 |
)
|
18 |
+
# The output is a list of dicts with 'generated_text'
|
19 |
+
return result[0]['generated_text']
|
|
|
|
|
20 |
|
21 |
+
# Create the Gradio interface
|
22 |
+
demo = gr.Interface(
|
23 |
+
fn=generate_text,
|
24 |
+
inputs=[
|
25 |
+
gr.Textbox(lines=4, label="Input Prompt"),
|
26 |
+
gr.Slider(32, 512, value=128, step=8, label="Max Length"),
|
27 |
+
gr.Slider(0.1, 2.0, value=1.0, step=0.05, label="Temperature"),
|
28 |
+
gr.Slider(0.5, 1.0, value=0.95, step=0.01, label="Top-p (nucleus sampling)")
|
29 |
+
],
|
30 |
+
outputs=gr.Textbox(lines=8, label="Generated Text"),
|
31 |
+
title="TinyLlama Text Generation",
|
32 |
+
description="Enter a prompt and generate text using TinyLlama/TinyLlama_v1.1."
|
33 |
+
)
|
34 |
|
35 |
+
# Launch the app
|
36 |
+
demo.launch()
|