HamidOmarov commited on
Commit
3acec7e
·
verified ·
1 Parent(s): a6f5647

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -17
app.py CHANGED
@@ -1,31 +1,47 @@
1
- import sys
2
  import os
 
3
  import gradio as gr
4
 
5
- # day3 qovluğunu sys.path-ə əlavə et
6
  sys.path.append(os.path.join(os.path.dirname(__file__), "day3"))
7
 
8
  from rag_system import RAGPipeline
9
 
10
- rag = RAGPipeline()
 
 
 
 
 
 
 
 
 
 
 
11
 
12
- def ask_pdf(pdf_file, question):
13
- if pdf_file:
14
- info = rag.index_document(pdf_file.name)
15
- else:
16
- info = {"chunks_indexed": 0, "best_strategy": None}
17
- out = rag.query(question)
18
- return f"**Answer:** {out['answer']}\n\n_Used chunks: {out['used_chunks']}_"
19
 
20
  demo = gr.Interface(
21
- fn=ask_pdf,
22
  inputs=[
23
- gr.File(label="Upload PDF", file_types=[".pdf"]),
24
- gr.Textbox(label="Question")
 
25
  ],
26
- outputs="markdown",
27
- title="PDF RAG System"
 
28
  )
29
 
30
- if __name__ == "__main__":
31
- demo.launch()
 
 
 
1
+ # app.py (repo root)
2
  import os
3
+ import sys
4
  import gradio as gr
5
 
6
+ # day3 qovluğunu import yoluna əlavə edək
7
  sys.path.append(os.path.join(os.path.dirname(__file__), "day3"))
8
 
9
  from rag_system import RAGPipeline
10
 
11
+ # Hugging Face Space üçün ayrıca persist directory
12
+ rag = RAGPipeline(persist_dir="./chroma_db_space", collection_name="pdf_docs")
13
+
14
+ def chat_with_pdf(pdf_path: str, question: str) -> str:
15
+ """
16
+ pdf_path: Gradio File input (type="filepath") tam fayl yolunu verir.
17
+ question: İstifadəçi sualı.
18
+ """
19
+ if not pdf_path:
20
+ return "Zəhmət olmasa PDF yüklə."
21
+ if not question or not question.strip():
22
+ return "Zəhmət olmasa sual daxil et."
23
 
24
+ # PDF-i indekslə (Chroma-ya yaz) və cavabı al
25
+ try:
26
+ rag.index_document(pdf_path, doc_id_prefix="upload")
27
+ out = rag.query(question, k=4)
28
+ return out["answer"]
29
+ except Exception as e:
30
+ return f"Xəta baş verdi: {e}"
31
 
32
  demo = gr.Interface(
33
+ fn=chat_with_pdf,
34
  inputs=[
35
+ # Vacib: type="filepath" → sadə string path qaytarır (Spaces-də schema problemi olmur)
36
+ gr.File(label="PDF yüklə", file_types=[".pdf"], type="filepath"),
37
+ gr.Textbox(label="Sual ver", placeholder="PDF nə deyir?")
38
  ],
39
+ outputs=gr.Textbox(label="Cavab"),
40
+ title="PDF RAG (Chroma + Groq)",
41
+ description="PDF yüklə və sual ver. Chroma ilə retrieval, Groq LLM ilə cavab."
42
  )
43
 
44
+ # Spaces-də launch çağırmırıq; yalnız lokal test üçün:
45
+ if __name__ == "__main__" and os.getenv("SPACE_ID") is None:
46
+ # Lokal işlədərkən bu xətti aktivdir
47
+ demo.launch(server_name="0.0.0.0", server_port=7860)