File size: 1,649 Bytes
3acec7e
b3617c5
3acec7e
e2677cb
 
3acec7e
b3617c5
e2677cb
b3617c5
 
3acec7e
 
 
 
 
 
 
 
 
 
 
 
b3617c5
3acec7e
 
 
 
 
 
 
e2677cb
 
3acec7e
b3617c5
3acec7e
 
 
b3617c5
3acec7e
 
 
e2677cb
 
3acec7e
 
 
 
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
# 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)