rajivmehtapy commited on
Commit
82341ad
1 Parent(s): 44e511b

update code for gpu

Browse files
Files changed (1) hide show
  1. app.py +24 -3
app.py CHANGED
@@ -1,7 +1,28 @@
 
 
1
  import gradio as gr
 
 
2
 
 
3
  def greet(name):
4
- return "Hello " + name + "!!"
 
 
 
 
 
 
 
 
 
5
 
6
- demo = gr.Interface(fn=greet, inputs="text", outputs="text")
7
- demo.launch()
 
 
 
 
 
 
 
 
1
+ import torch
2
+ from transformers import pipeline
3
  import gradio as gr
4
+ import spaces
5
+ from transformers import AutoTokenizer, AutoModelForCausalLM
6
 
7
+ @spaces.GPU
8
  def greet(name):
9
+ pipe = pipeline("text-generation", model="TinyLlama/TinyLlama-1.1B-Chat-v1.0", torch_dtype=torch.bfloat16, device_map="auto")
10
+ messages = [
11
+ {
12
+ "role": "system",
13
+ "content": "You are a friendly chatbot who always responds in the Professsional way",
14
+ },
15
+ {"role": "user", "content": name},
16
+ ]
17
+ prompt = pipe.tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
18
+ outputs = pipe(prompt, max_new_tokens=256, do_sample=True, temperature=0.7, top_k=50, top_p=0.95)
19
 
20
+ return outputs[0]["generated_text"]
21
+
22
+ demo = gr.Interface(
23
+ fn=greet,
24
+ inputs=["text"],
25
+ outputs=["text"],
26
+ )
27
+
28
+ demo.launch(share=True)