Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 3 |
+
import torch
|
| 4 |
+
|
| 5 |
+
# ==========================
|
| 6 |
+
# 1. Carica il modello italiano
|
| 7 |
+
# ==========================
|
| 8 |
+
MODEL_NAME = "Mattimax/DACMini-IT"
|
| 9 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 10 |
+
|
| 11 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
| 12 |
+
model = AutoModelForCausalLM.from_pretrained(MODEL_NAME).to(device)
|
| 13 |
+
|
| 14 |
+
# ==========================
|
| 15 |
+
# 2. Lista di argomenti validi
|
| 16 |
+
# ==========================
|
| 17 |
+
# Tutto ciò che è inerente al negozio (es. prodotti, orari, contatti, marchi)
|
| 18 |
+
VALID_KEYWORDS = [
|
| 19 |
+
"intimo", "calze", "collant", "pigiami", "homewear",
|
| 20 |
+
"merceria", "bottoni", "fili", "nastri", "orari",
|
| 21 |
+
"telefono", "email", "marchi", "logo", "contatti"
|
| 22 |
+
]
|
| 23 |
+
|
| 24 |
+
# ==========================
|
| 25 |
+
# 3. Funzione di risposta con filtro
|
| 26 |
+
# ==========================
|
| 27 |
+
def answer_question(user_input):
|
| 28 |
+
# verifica se la domanda contiene almeno una keyword valida
|
| 29 |
+
if not any(keyword.lower() in user_input.lower() for keyword in VALID_KEYWORDS):
|
| 30 |
+
return "Non posso risponderti a questa domanda."
|
| 31 |
+
|
| 32 |
+
# prepara input per il modello
|
| 33 |
+
inputs = tokenizer(user_input, return_tensors="pt").to(device)
|
| 34 |
+
|
| 35 |
+
# genera la risposta
|
| 36 |
+
with torch.no_grad():
|
| 37 |
+
outputs = model.generate(
|
| 38 |
+
**inputs,
|
| 39 |
+
max_new_tokens=100,
|
| 40 |
+
do_sample=True,
|
| 41 |
+
top_p=0.9,
|
| 42 |
+
temperature=0.7
|
| 43 |
+
)
|
| 44 |
+
|
| 45 |
+
# decodifica la risposta
|
| 46 |
+
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 47 |
+
return response
|
| 48 |
+
|
| 49 |
+
# ==========================
|
| 50 |
+
# 4. Interfaccia Gradio
|
| 51 |
+
# ==========================
|
| 52 |
+
iface = gr.Interface(
|
| 53 |
+
fn=answer_question,
|
| 54 |
+
inputs=gr.Textbox(lines=2, placeholder="Scrivi la tua domanda sul negozio..."),
|
| 55 |
+
outputs=gr.Textbox(label="Risposta"),
|
| 56 |
+
title="Assistente Merceria Galli",
|
| 57 |
+
description="Chiedimi solo informazioni sui prodotti e servizi della Merceria Galli."
|
| 58 |
+
)
|
| 59 |
+
|
| 60 |
+
if __name__ == "__main__":
|
| 61 |
+
iface.launch()
|