import gradio as gr from transformers import pipeline # 加載Hugging Face上的語意檢索模型 qa_pipeline = pipeline("question-answering", model="deepset/roberta-base-squad2") def answer_question(document, question): # 模擬基於上傳文件內容進行的問答 response = qa_pipeline({'question': question, 'context': document}) return response['answer'] # 定義 Gradio UI def chat_interface(): with gr.Blocks() as demo: # 文件上傳元件 document = gr.Textbox(label="Document Text", lines=10, placeholder="Paste the content of the document here...") question = gr.Textbox(label="Ask a question about the document") answer = gr.Textbox(label="Answer") # 按鈕用於觸發回答 gr.Button("Ask").click(fn=answer_question, inputs=[document, question], outputs=answer) return demo demo = chat_interface() demo.launch(share=True)