kdevoe commited on
Commit
157f397
·
1 Parent(s): 8fe5ece

Adding streaming

Browse files
Files changed (1) hide show
  1. app.py +15 -15
app.py CHANGED
@@ -1,27 +1,27 @@
1
  import gradio as gr
2
- import time
3
- from transformers import pipeline
4
 
5
- # Load the TinyLlama text generation pipeline
6
- pipe = pipeline("text-generation", model="TinyLlama/TinyLlama-1.1B-Chat-v1.0")
 
 
7
 
8
- # Define the inference function
 
 
 
9
  def generate_text(prompt):
10
- start_time = time.time()
11
- results = pipe(prompt, max_length=100, num_return_sequences=1)
12
- end_time = time.time()
13
- response_time = end_time - start_time
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
- gr.Textbox(label="Generated Text"),
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