Adding streaming
Browse files
app.py
CHANGED
|
@@ -1,27 +1,27 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import
|
| 3 |
-
from transformers import pipeline
|
| 4 |
|
| 5 |
-
# Load the
|
| 6 |
-
|
|
|
|
|
|
|
| 7 |
|
| 8 |
-
#
|
|
|
|
|
|
|
|
|
|
| 9 |
def generate_text(prompt):
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
return results[0]['generated_text'], f"{response_time:.2f} seconds"
|
| 15 |
|
| 16 |
# Create a Gradio interface
|
| 17 |
iface = gr.Interface(
|
| 18 |
fn=generate_text,
|
| 19 |
inputs=gr.Textbox(lines=2, placeholder="Enter your prompt here..."),
|
| 20 |
-
outputs=
|
| 21 |
-
|
| 22 |
-
gr.Textbox(label="Response Time")
|
| 23 |
-
],
|
| 24 |
-
title="TinyLlama Text Generation"
|
| 25 |
)
|
| 26 |
|
| 27 |
# Launch the interface
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM, TextGenerationPipeline
|
|
|
|
| 3 |
|
| 4 |
+
# Load the model and tokenizer
|
| 5 |
+
model_name = "TinyLlama/TinyLlama-1.1B-Chat-v1.0"
|
| 6 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 7 |
+
model = AutoModelForCausalLM.from_pretrained(model_name)
|
| 8 |
|
| 9 |
+
# Create a text generation pipeline
|
| 10 |
+
pipeline = TextGenerationPipeline(model=model, tokenizer=tokenizer)
|
| 11 |
+
|
| 12 |
+
# Define the inference function with streaming
|
| 13 |
def generate_text(prompt):
|
| 14 |
+
output = ""
|
| 15 |
+
for token in pipeline(prompt, max_length=100, num_return_sequences=1, do_sample=True, top_k=50, top_p=0.95):
|
| 16 |
+
output += token['generated_text']
|
| 17 |
+
yield output # Stream the output
|
|
|
|
| 18 |
|
| 19 |
# Create a Gradio interface
|
| 20 |
iface = gr.Interface(
|
| 21 |
fn=generate_text,
|
| 22 |
inputs=gr.Textbox(lines=2, placeholder="Enter your prompt here..."),
|
| 23 |
+
outputs=gr.Textbox(label="Generated Text"),
|
| 24 |
+
live=True # Enable live streaming
|
|
|
|
|
|
|
|
|
|
| 25 |
)
|
| 26 |
|
| 27 |
# Launch the interface
|