-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
60 lines (47 loc) · 1.74 KB
/
app.py
File metadata and controls
60 lines (47 loc) · 1.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import gradio as gr
from transformers import pipeline
# Load multilingual model
qa_pipeline = pipeline("question-answering", model="mrm8488/bert-multi-cased-finetuned-xquadv1")
# Chat history
chat_history = []
def read_file(file_obj):
if file_obj is None:
return ""
filename = file_obj.name if hasattr(file_obj, 'name') else str(file_obj)
if filename.endswith(".txt"):
with open(file_obj, "r", encoding="utf-8") as f:
return f.read()
elif filename.endswith(".pdf"):
import PyPDF2
with open(file_obj, "rb") as f:
reader = PyPDF2.PdfReader(f)
text = ""
for page in reader.pages:
text += page.extract_text()
return text
elif filename.endswith(".docx"):
import docx
doc = docx.Document(file_obj)
return "\n".join([p.text for p in doc.paragraphs])
else:
return "Unsupported file type."
def chat(user_question, file, history):
context = read_file(file)
if not user_question or not context:
return history + [[user_question, "Please provide a question and valid file."]]
try:
result = qa_pipeline(question=user_question, context=context)
answer = result["answer"]
except Exception as e:
answer = f"Error: {repr(e)}"
history.append([user_question, answer])
return history
with gr.Blocks() as demo:
gr.Markdown("## 🌍 Multilingual Q&A Chatbot with File Upload")
chatbot = gr.Chatbot(label="Conversation")
with gr.Row():
txt = gr.Textbox(label="Ask a question")
file = gr.File(label="Upload PDF, TXT, or DOCX")
submit = gr.Button("Submit")
submit.click(chat, inputs=[txt, file, chatbot], outputs=chatbot)
demo.launch()