Spaces:
Configuration error
Configuration error
import gradio as gr | |
from rag_pipeline import ArabicRAGSystem | |
from document_processor import process_pdf, process_docx | |
import os | |
rag = ArabicRAGSystem() | |
def process_uploaded_files(files): | |
"""Handle uploaded documents""" | |
all_chunks = [] | |
for file in files: | |
if file.name.endswith('.pdf'): | |
chunks = process_pdf(file.name) | |
elif file.name.endswith('.docx'): | |
chunks = process_docx(file.name) | |
all_chunks.extend(chunks) | |
if all_chunks: | |
rag.build_index(all_chunks) | |
return "تم تحميل المستندات بنجاح! يمكنك الآن طرح الأسئلة." | |
return "حدث خطأ في معالجة الملفات." | |
def respond(question, history): | |
"""Generate response to user question""" | |
if not rag.index: | |
return "الرجاء تحميل المستندات أولاً" | |
context = rag.retrieve(question) | |
answer = rag.generate_answer(question, context) | |
cited_answer = f"{answer}\n\nالمصادر:\n" + "\n".join( | |
f"- {c[:100]}..." for c in context | |
) | |
return cited_answer | |
with gr.Blocks(title="نظام الدردشة العربي المدعوم بالوثائق", theme=gr.themes.Soft()) as demo: | |
gr.Markdown("## نظام الدردشة العربي المدعوم بالوثائق") | |
with gr.Row(): | |
with gr.Column(): | |
file_output = gr.File(label="تحميل المستندات", file_count="multiple") | |
upload_button = gr.Button("معالجة الملفات") | |
upload_status = gr.Textbox(label="حالة التحميل") | |
with gr.Column(): | |
chatbot = gr.Chatbot(height=400) | |
question = gr.Textbox(label="اكتب سؤالك هنا") | |
submit = gr.Button("إرسال") | |
upload_button.click( | |
process_uploaded_files, | |
inputs=file_output, | |
outputs=upload_status | |
) | |
submit.click( | |
respond, | |
inputs=[question, chatbot], | |
outputs=chatbot | |
) | |
question.submit( | |
respond, | |
inputs=[question, chatbot], | |
outputs=chatbot | |
) | |
if __name__ == "__main__": | |
demo.launch() | |