kdevoe commited on
Commit
75f5166
·
1 Parent(s): 80d593f

Reverting back to original version

Browse files
Files changed (1) hide show
  1. app.py +16 -19
app.py CHANGED
@@ -1,30 +1,27 @@
1
  import gradio as gr
2
- from transformers import AutoTokenizer, AutoModelForCausalLM, TextGenerationPipeline
3
- import asyncio
4
 
5
- # Load the model and tokenizer
6
- model_name = "TinyLlama/TinyLlama-1.1B-Chat-v1.0"
7
- tokenizer = AutoTokenizer.from_pretrained(model_name)
8
- model = AutoModelForCausalLM.from_pretrained(model_name)
9
 
10
- # Create a text generation pipeline
11
- pipeline = TextGenerationPipeline(model=model, tokenizer=tokenizer)
12
-
13
- # Define the inference function with streaming
14
- async def generate_text(prompt):
15
- loop = asyncio.get_event_loop()
16
- output = ""
17
- for token in pipeline(prompt, max_length=100, num_return_sequences=1, do_sample=True, top_k=50, top_p=0.95):
18
- output += token['generated_text']
19
- yield output # Stream the output
20
- await loop.run_in_executor(None, lambda: None) # Yield control to the event loop
21
 
22
  # Create a Gradio interface
23
  iface = gr.Interface(
24
  fn=generate_text,
25
  inputs=gr.Textbox(lines=2, placeholder="Enter your prompt here..."),
26
- outputs=gr.Textbox(label="Generated Text"),
27
- live=True # Enable live streaming
 
 
 
28
  )
29
 
30
  # Launch the interface
 
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