Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 4 |
+
|
| 5 |
+
MODEL_ID = "Hodely/AmInSide-1.0"
|
| 6 |
+
|
| 7 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
|
| 8 |
+
|
| 9 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 10 |
+
MODEL_ID,
|
| 11 |
+
torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32
|
| 12 |
+
)
|
| 13 |
+
|
| 14 |
+
if tokenizer.pad_token is None:
|
| 15 |
+
tokenizer.pad_token = tokenizer.eos_token
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
SYSTEM_PROMPT = (
|
| 19 |
+
"Eres AmInSide 1.0, una IA conversacional 煤til, clara, creativa e inteligente. "
|
| 20 |
+
"Responde de forma natural, bien escrita y precisa."
|
| 21 |
+
)
|
| 22 |
+
|
| 23 |
+
def build_prompt(message, history):
|
| 24 |
+
prompt = SYSTEM_PROMPT + "\n\n"
|
| 25 |
+
for user_msg, bot_msg in history:
|
| 26 |
+
prompt += f"Usuario: {user_msg}\nAsistente: {bot_msg}\n"
|
| 27 |
+
prompt += f"Usuario: {message}\nAsistente:"
|
| 28 |
+
return prompt
|
| 29 |
+
|
| 30 |
+
def chat(message, history):
|
| 31 |
+
history = history or []
|
| 32 |
+
prompt = build_prompt(message, history)
|
| 33 |
+
|
| 34 |
+
inputs = tokenizer(prompt, return_tensors="pt", truncation=True, max_length=1024)
|
| 35 |
+
inputs = {k: v.to(model.device) for k, v in inputs.items()}
|
| 36 |
+
|
| 37 |
+
with torch.no_grad():
|
| 38 |
+
output = model.generate(
|
| 39 |
+
**inputs,
|
| 40 |
+
max_new_tokens=180,
|
| 41 |
+
temperature=0.8,
|
| 42 |
+
do_sample=True,
|
| 43 |
+
top_p=0.9,
|
| 44 |
+
repetition_penalty=1.1,
|
| 45 |
+
pad_token_id=tokenizer.eos_token_id
|
| 46 |
+
)
|
| 47 |
+
|
| 48 |
+
full_text = tokenizer.decode(output[0], skip_special_tokens=True)
|
| 49 |
+
|
| 50 |
+
if "Asistente:" in full_text:
|
| 51 |
+
response = full_text.split("Asistente:")[-1].strip()
|
| 52 |
+
else:
|
| 53 |
+
response = full_text[len(prompt):].strip()
|
| 54 |
+
|
| 55 |
+
history.append((message, response))
|
| 56 |
+
return history, history
|
| 57 |
+
|
| 58 |
+
with gr.Blocks(title="AmInSide 1.0") as demo:
|
| 59 |
+
gr.Markdown("# AmInSide 1.0")
|
| 60 |
+
gr.Markdown("Chat de demostraci贸n del modelo.")
|
| 61 |
+
|
| 62 |
+
chatbot = gr.Chatbot(height=500)
|
| 63 |
+
msg = gr.Textbox(label="Escribe tu mensaje", placeholder="Habla con AmInSide 1.0...")
|
| 64 |
+
state = gr.State([])
|
| 65 |
+
|
| 66 |
+
msg.submit(chat, inputs=[msg, state], outputs=[chatbot, state])
|
| 67 |
+
msg.submit(lambda: "", None, msg)
|
| 68 |
+
|
| 69 |
+
demo.launch()
|