Spaces:
Sleeping
Sleeping
# app.py (repo root) | |
import os | |
import sys | |
import gradio as gr | |
# day3 qovluğunu import yoluna əlavə edək | |
sys.path.append(os.path.join(os.path.dirname(__file__), "day3")) | |
from rag_system import RAGPipeline | |
# Hugging Face Space üçün ayrıca persist directory | |
rag = RAGPipeline(persist_dir="./chroma_db_space", collection_name="pdf_docs") | |
def chat_with_pdf(pdf_path: str, question: str) -> str: | |
""" | |
pdf_path: Gradio File input (type="filepath") tam fayl yolunu verir. | |
question: İstifadəçi sualı. | |
""" | |
if not pdf_path: | |
return "Zəhmət olmasa PDF yüklə." | |
if not question or not question.strip(): | |
return "Zəhmət olmasa sual daxil et." | |
# PDF-i indekslə (Chroma-ya yaz) və cavabı al | |
try: | |
rag.index_document(pdf_path, doc_id_prefix="upload") | |
out = rag.query(question, k=4) | |
return out["answer"] | |
except Exception as e: | |
return f"Xəta baş verdi: {e}" | |
demo = gr.Interface( | |
fn=chat_with_pdf, | |
inputs=[ | |
# Vacib: type="filepath" → sadə string path qaytarır (Spaces-də schema problemi olmur) | |
gr.File(label="PDF yüklə", file_types=[".pdf"], type="filepath"), | |
gr.Textbox(label="Sual ver", placeholder="PDF nə deyir?") | |
], | |
outputs=gr.Textbox(label="Cavab"), | |
title="PDF RAG (Chroma + Groq)", | |
description="PDF yüklə və sual ver. Chroma ilə retrieval, Groq LLM ilə cavab." | |
) | |
# Spaces-də launch çağırmırıq; yalnız lokal test üçün: | |
if __name__ == "__main__" and os.getenv("SPACE_ID") is None: | |
# Lokal işlədərkən bu xətti aktivdir | |
demo.launch(server_name="0.0.0.0", server_port=7860) | |