ItsMatti4 commited on
Commit
3ffab8d
·
verified ·
1 Parent(s): bd8800b

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -0
app.py ADDED
@@ -0,0 +1,39 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
3
+ import torch
4
+
5
+ model_id = "TinyLlama/TinyLlama-1.1B-Chat-v1.0"
6
+
7
+ # carica modello (ottimizzato CPU)
8
+ tokenizer = AutoTokenizer.from_pretrained(model_id)
9
+ model = AutoModelForCausalLM.from_pretrained(
10
+ model_id,
11
+ torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
12
+ device_map="auto"
13
+ )
14
+
15
+ pipe = pipeline(
16
+ "text-generation",
17
+ model=model,
18
+ tokenizer=tokenizer
19
+ )
20
+
21
+ def chat(message, history):
22
+ prompt = ""
23
+ for user, bot in history:
24
+ prompt += f"User: {user}\nAssistant: {bot}\n"
25
+ prompt += f"User: {message}\nAssistant:"
26
+
27
+ output = pipe(
28
+ prompt,
29
+ max_new_tokens=120,
30
+ do_sample=True,
31
+ temperature=0.7
32
+ )
33
+
34
+ response = output[0]["generated_text"].split("Assistant:")[-1].strip()
35
+ return response
36
+
37
+ demo = gr.ChatInterface(fn=chat, title="Nexa 0.1 🔥")
38
+
39
+ demo.launch()