Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,43 +1,33 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from transformers import AutoTokenizer,
|
| 3 |
import torch
|
| 4 |
|
| 5 |
-
# Load model
|
| 6 |
-
model_name = "
|
| 7 |
-
tokenizer = AutoTokenizer.from_pretrained(model_name
|
| 8 |
-
model =
|
| 9 |
|
| 10 |
-
#
|
| 11 |
persona_prompts = {
|
| 12 |
-
"Elon Musk": "
|
| 13 |
-
"Jensen Huang": "
|
| 14 |
-
"Jeff Bezos": "
|
| 15 |
}
|
| 16 |
|
| 17 |
-
def format_openchat_prompt(system_prompt, user_input):
|
| 18 |
-
return f"<|system|>\n{system_prompt}\n<|end|>\n<|user|>\n{user_input}\n<|end|>\n<|assistant|>\n"
|
| 19 |
-
|
| 20 |
def chatbot(persona, input_text):
|
| 21 |
-
|
| 22 |
-
prompt = format_openchat_prompt(system_prompt, input_text)
|
| 23 |
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
| 24 |
|
| 25 |
-
pad_token_id = tokenizer.pad_token_id if tokenizer.pad_token_id is not None else tokenizer.eos_token_id
|
| 26 |
-
|
| 27 |
with torch.no_grad():
|
| 28 |
outputs = model.generate(
|
| 29 |
**inputs,
|
| 30 |
-
max_new_tokens=
|
| 31 |
do_sample=True,
|
| 32 |
temperature=0.7,
|
| 33 |
-
top_p=0.9
|
| 34 |
-
eos_token_id=tokenizer.eos_token_id,
|
| 35 |
-
pad_token_id=pad_token_id
|
| 36 |
)
|
| 37 |
|
| 38 |
-
|
| 39 |
-
assistant_response = full_output.split("<|assistant|>")[-1].strip()
|
| 40 |
-
return assistant_response
|
| 41 |
|
| 42 |
# Gradio UI
|
| 43 |
iface = gr.Interface(
|
|
@@ -47,8 +37,8 @@ iface = gr.Interface(
|
|
| 47 |
gr.Textbox(lines=2, placeholder="Ask something...")
|
| 48 |
],
|
| 49 |
outputs="text",
|
| 50 |
-
title="Persona Bot (
|
| 51 |
-
description="Chat with
|
| 52 |
)
|
| 53 |
|
| 54 |
-
iface.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
| 3 |
import torch
|
| 4 |
|
| 5 |
+
# Load lightweight model
|
| 6 |
+
model_name = "google/flan-t5-small"
|
| 7 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 8 |
+
model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
|
| 9 |
|
| 10 |
+
# Simple persona instructions
|
| 11 |
persona_prompts = {
|
| 12 |
+
"Elon Musk": "As Elon Musk, visionary tech entrepreneur, answer: ",
|
| 13 |
+
"Jensen Huang": "As Jensen Huang, AI hardware innovator, answer: ",
|
| 14 |
+
"Jeff Bezos": "As Jeff Bezos, strategic business leader, answer: "
|
| 15 |
}
|
| 16 |
|
|
|
|
|
|
|
|
|
|
| 17 |
def chatbot(persona, input_text):
|
| 18 |
+
prompt = persona_prompts.get(persona, "") + input_text
|
|
|
|
| 19 |
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
| 20 |
|
|
|
|
|
|
|
| 21 |
with torch.no_grad():
|
| 22 |
outputs = model.generate(
|
| 23 |
**inputs,
|
| 24 |
+
max_new_tokens=128,
|
| 25 |
do_sample=True,
|
| 26 |
temperature=0.7,
|
| 27 |
+
top_p=0.9
|
|
|
|
|
|
|
| 28 |
)
|
| 29 |
|
| 30 |
+
return tokenizer.decode(outputs[0], skip_special_tokens=True)
|
|
|
|
|
|
|
| 31 |
|
| 32 |
# Gradio UI
|
| 33 |
iface = gr.Interface(
|
|
|
|
| 37 |
gr.Textbox(lines=2, placeholder="Ask something...")
|
| 38 |
],
|
| 39 |
outputs="text",
|
| 40 |
+
title="Persona Bot (Fast Mode)",
|
| 41 |
+
description="Chat quickly with Elon Musk, Jensen Huang, or Jeff Bezos using a lightweight model.",
|
| 42 |
)
|
| 43 |
|
| 44 |
+
iface.launch(share=True)
|