| import gradio as gr |
| from rapidfuzz import process |
|
|
| |
| |
| |
| saludos = ["azul", "azul amddukel", "sabah", "sba7 lkhir", "thensid di lkhir"] |
| preguntas = ["manec t-llid?", "mamech t-llid?", "mamec t-llid?", "manis", "mamec dach qqarn?"] |
| despedidas = ["ar nemzar", "tanemmirt", "yallah"] |
| confirmaciones = ["wah", "lla", "mara tekhsed"] |
| disculpas = ["sam7ahi"] |
|
|
| respuestas = { |
| "saludo": "Azul, amddukel! Manec t-llid?", |
| "pregunta": "Tanemmirt, i cek, mamech t-llid?", |
| "despedida": "Ar nemzar.", |
| "confirmacion": "Wah, fhimegh!", |
| "disculpa": "War din bu lmuchkil!", |
| "desconocido": "War fhimegh. Tabrat tiwed." |
| } |
|
|
| todas = saludos + preguntas + despedidas + confirmaciones + disculpas |
|
|
| |
| |
| |
| def categorizar(mensaje): |
| p = mensaje.lower().strip() |
| if p in saludos: return "saludo" |
| if p in preguntas: return "pregunta" |
| if p in despedidas: return "despedida" |
| if p in confirmaciones: return "confirmacion" |
| if p in disculpas: return "disculpa" |
| return "desconocido" |
|
|
| def responder(mensaje, chat_history): |
| p = mensaje.lower().strip() |
| resultado = process.extractOne(p, todas, score_cutoff=80) |
| if resultado: |
| p = resultado[0] |
|
|
| categoria = categorizar(p) |
| respuesta = respuestas[categoria] |
|
|
| chat_history = chat_history or [] |
| chat_history.append({"role": "user", "content": mensaje}) |
| chat_history.append({"role": "assistant", "content": respuesta}) |
| return chat_history |
|
|
| |
| |
| |
| with gr.Blocks(title="Tawalt π") as demo: |
| gr.Markdown("<h2 style='color:#1E90FF'>Tawalt π β Tarifit Berber NLP Chat</h2>") |
|
|
| chatbot = gr.Chatbot(elem_id="chatbot", height=400) |
|
|
| with gr.Row(): |
| mensaje = gr.Textbox(placeholder="azul, sba7 lkhir, sam7ahi...", label="") |
| enviar = gr.Button("Enviar", elem_id="send_btn") |
|
|
| enviar.click(responder, inputs=[mensaje, chatbot], outputs=chatbot) |
| mensaje.submit(responder, inputs=[mensaje, chatbot], outputs=chatbot) |
|
|
| |
| |
| |
| demo.launch( |
| server_name="0.0.0.0", |
| server_port=7860, |
| show_error=True |
| ) |