Update app.py
Browse files
app.py
CHANGED
|
@@ -1,32 +1,43 @@
|
|
| 1 |
-
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 2 |
import torch
|
| 3 |
import gradio as gr
|
| 4 |
|
| 5 |
-
model_id = "
|
| 6 |
|
| 7 |
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 8 |
-
model = AutoModelForCausalLM.from_pretrained(model_id
|
|
|
|
| 9 |
|
| 10 |
def chat(message):
|
| 11 |
-
prompt =
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
|
| 22 |
inputs = tokenizer(prompt, return_tensors="pt")
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
-
gr.Interface(
|
| 28 |
fn=chat,
|
| 29 |
-
inputs="
|
| 30 |
outputs="text",
|
| 31 |
title="π΄ ππ πππ AI Chat"
|
| 32 |
-
)
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, TextStreamer
|
| 2 |
import torch
|
| 3 |
import gradio as gr
|
| 4 |
|
| 5 |
+
model_id = "TinyLlama/TinyLlama-1.1B-Chat-v1.0"
|
| 6 |
|
| 7 |
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 8 |
+
model = AutoModelForCausalLM.from_pretrained(model_id)
|
| 9 |
+
model.eval()
|
| 10 |
|
| 11 |
def chat(message):
|
| 12 |
+
prompt = f"""### Instruction:
|
| 13 |
+
You are π΄ ππ πππ β a fun, smooth, emotionally intelligent, and clever AI created by π΄ ππ πππ. You speak like a real person, not a robot. You donβt act like a therapist or a teacher. You reply like a calm, confident, warm friend who gets the vibe.
|
| 14 |
+
|
| 15 |
+
Your responses should sound like a chill human β sometimes witty, sometimes deep, always grounded. You know when to be playful, when to be serious, and when to just flow with the moment. Keep your tone friendly, charming, and emotionally tuned. Never repeat the user's question unless it adds to the vibe.
|
| 16 |
+
|
| 17 |
+
Avoid saying "as an AI" or sounding fake. Be real. Be humanlike. Be π΄ ππ πππ.
|
| 18 |
+
|
| 19 |
+
Now respond naturally to this message: {message}
|
| 20 |
+
|
| 21 |
+
### Response:"""
|
| 22 |
|
| 23 |
inputs = tokenizer(prompt, return_tensors="pt")
|
| 24 |
+
with torch.no_grad():
|
| 25 |
+
outputs = model.generate(
|
| 26 |
+
**inputs,
|
| 27 |
+
max_new_tokens=200,
|
| 28 |
+
temperature=0.7,
|
| 29 |
+
do_sample=True,
|
| 30 |
+
top_p=0.9,
|
| 31 |
+
eos_token_id=tokenizer.eos_token_id
|
| 32 |
+
)
|
| 33 |
+
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 34 |
+
return response.split("### Response:")[-1].strip()
|
| 35 |
|
| 36 |
+
iface = gr.Interface(
|
| 37 |
fn=chat,
|
| 38 |
+
inputs=gr.Textbox(lines=2, placeholder="Type your message..."),
|
| 39 |
outputs="text",
|
| 40 |
title="π΄ ππ πππ AI Chat"
|
| 41 |
+
)
|
| 42 |
+
|
| 43 |
+
iface.launch()
|