Erik22TY commited on
Commit
38d0bd1
·
verified ·
1 Parent(s): 928a03f

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -0
app.py ADDED
@@ -0,0 +1,44 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
3
+ import torch
4
+
5
+ # only the real FLAN-T5 Small model repo
6
+ MODEL_REPO = "google/flan-t5-small"
7
+
8
+ print("Loading FLAN-T5 Small model...")
9
+ tokenizer = AutoTokenizer.from_pretrained(MODEL_REPO)
10
+ model = AutoModelForSeq2SeqLM.from_pretrained(MODEL_REPO)
11
+ model.eval()
12
+ print("Model loaded")
13
+
14
+ def chat(prompt):
15
+ if not prompt.strip():
16
+ return "Type a message first"
17
+ inputs = tokenizer(
18
+ prompt,
19
+ return_tensors="pt",
20
+ truncation=True,
21
+ padding=True,
22
+ max_length=256
23
+ )
24
+ with torch.no_grad():
25
+ output = model.generate(
26
+ **inputs,
27
+ max_length=128,
28
+ do_sample=True,
29
+ top_p=0.9,
30
+ temperature=0.7
31
+ )
32
+ return tokenizer.decode(output[0], skip_special_tokens=True)
33
+
34
+ with gr.Blocks(title="SmallGPT CPU") as demo:
35
+ gr.Markdown("# SmallGPT CPU Chat \nPowered by google/flan-t5-small (CPU only)")
36
+
37
+ user_input = gr.Textbox(label="Your message", lines=2)
38
+ bot_output = gr.Textbox(label="SmallGPT says", lines=4)
39
+
40
+ send_btn = gr.Button("Send")
41
+ send_btn.click(chat, inputs=user_input, outputs=bot_output)
42
+ user_input.submit(chat, inputs=user_input, outputs=bot_output)
43
+
44
+ demo.launch()