Reverting back to original version
Browse files123456789101112131415161718192021222324252627282930import gradio as grfrom transformers import AutoTokenizer, AutoModelForCausalLM, TextGenerationPipelineimport asyncio# Load the model and tokenizermodel_name = "TinyLlama/TinyLlama-1.1B-Chat-v1.0"tokenizer = AutoTokenizer.from_pretrained(model_name)model = AutoModelForCausalLM.from_pretrained(model_name)# Create a text generation pipelinepipeline = TextGenerationPipeline(model=model, tokenizer=tokenizer)# Define the inference function with streamingasync def generate_text(prompt):loop = asyncio.get_event_loop()output = ""for token in pipeline(prompt, max_length=100, num_return_sequences=1, do_sample=True, top_k=50, top_p=0.95):output += token['generated_text']yield output # Stream the outputawait loop.run_in_executor(None, lambda: None) # Yield control to the event loop# Create a Gradio interfaceiface = gr.Interface(fn=generate_text,inputs=gr.Textbox(lines=2, placeholder="Enter your prompt here..."),outputs=gr.Textbox(label="Generated Text"),live=True # Enable live streaming)# Launch the interface123456789101112131415161718192021222324252627import gradio as grimport timefrom transformers import pipeline# Load the TinyLlama text generation pipelinepipe = pipeline("text-generation", model="TinyLlama/TinyLlama-1.1B-Chat-v1.0")# Define the inference functiondef generate_text(prompt):start_time = time.time()results = pipe(prompt, max_length=100, num_return_sequences=1)end_time = time.time()response_time = end_time - start_timereturn results[0]['generated_text'], f"{response_time:.2f} seconds"# Create a Gradio interfaceiface = gr.Interface(fn=generate_text,inputs=gr.Textbox(lines=2, placeholder="Enter your prompt here..."),outputs=[gr.Textbox(label="Generated Text"),gr.Textbox(label="Response Time")],title="TinyLlama Text Generation")# Launch the interface
app.py+16-19