import gradio as gr from transformers import pipeline from difflib import SequenceMatcher with open("my_document.txt", "r") as f: document_text = f.read() question_input = gr.inputs.Textbox(label="Inserisci la tua domanda qui") qa_model = pipeline("question-answering", model="timpal0l/mdeberta-v3-base-squad2") def similar_questions(question, question_list): max_ratio = 0 best_question = "" for q in question_list: ratio = SequenceMatcher(None, question.lower(), q.lower()).ratio() if ratio > max_ratio: max_ratio = ratio best_question = q return best_question if max_ratio >= 0.7 else None def answer_question(question): ground_truth_questions = ["Qual è la capitale dell'Italia?", "Chi ha scritto la Divina Commedia?", "Qual è il fiume più lungo del mondo?"] similar_question = similar_questions(question, ground_truth_questions) if similar_question: if similar_question == "Qual è la capitale dell'Italia?": return "Roma" elif similar_question == "Chi ha scritto la Divina Commedia?": return "Dante Alighieri" elif similar_question == "Qual è il fiume più lungo del mondo?": return "Nilo" else: answer = qa_model(question=question, context=document_text, max_answer_len=200) return answer["answer"] gr.Interface(fn=answer_question, inputs=question_input, outputs="text").launch()