João Lima commited on
Commit
87fe6ce
·
1 Parent(s): 39e8d67

updating app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -45
app.py CHANGED
@@ -4,49 +4,49 @@ from rag.pipeline import run_rag
4
 
5
  vectorstore = None
6
 
7
- def load_document(file):
8
  global vectorstore
9
- if file is None:
10
- return "Please upload a PDF file."
11
- try:
12
- vectorstore = process_pdf(file.name)
13
- return "✓ Document processed successfully."
14
- except Exception as e:
15
- return f"❌ Error: {str(e)}"
16
-
17
- def ask(question):
18
- if vectorstore is None:
19
- return " Upload a document first", "", ""
20
- if not question.strip():
21
- return " Please enter a question", "", ""
22
- try:
23
- return run_rag(question, vectorstore)
24
- except Exception as e:
25
- return f"❌ Error: {str(e)}", "", ""
26
-
27
- with gr.Blocks(title="Tech Explainer RAG") as demo:
28
- gr.Markdown("# Tech Explainer — RAG with Automatic Evaluation")
29
-
30
- file = gr.File(label="Upload PDF", file_types=[".pdf"])
31
- load_btn = gr.Button("Process PDF")
32
- status = gr.Textbox(label="Status")
33
-
34
- gr.Markdown("---")
35
-
36
- question = gr.Textbox(label="Question", placeholder="Ask a question about the document...")
37
- ask_btn = gr.Button("Ask")
38
-
39
- answer = gr.Textbox(label="Answer", lines=5)
40
- sources = gr.Textbox(label="Sources", lines=2)
41
- evaluation = gr.Textbox(label="Evaluation", lines=3)
42
-
43
- load_btn.click(load_document, inputs=file, outputs=status)
44
- ask_btn.click(ask, inputs=question, outputs=[answer, sources, evaluation])
45
-
46
- demo.launch(
47
- share=False,
48
- server_name="0.0.0.0",
49
- server_port=7860,
50
- show_error=True,
51
- quiet=False
52
- )
 
4
 
5
  vectorstore = None
6
 
7
+ def process_and_query(file, question):
8
  global vectorstore
9
+
10
+ status = ""
11
+ answer = ""
12
+ sources = ""
13
+ evaluation = ""
14
+
15
+ # Processar PDF se fornecido
16
+ if file is not None:
17
+ try:
18
+ vectorstore = process_pdf(file.name)
19
+ status = " Document processed successfully"
20
+ except Exception as e:
21
+ return f" Error processing PDF: {str(e)}", "", "", ""
22
+
23
+ # Responder pergunta
24
+ if question and question.strip():
25
+ if vectorstore is None:
26
+ answer = "⚠ Please upload a PDF first"
27
+ else:
28
+ try:
29
+ answer, sources, evaluation = run_rag(question, vectorstore)
30
+ except Exception as e:
31
+ answer = f" Error: {str(e)}"
32
+
33
+ return status, answer, sources, evaluation
34
+
35
+ demo = gr.Interface(
36
+ fn=process_and_query,
37
+ inputs=[
38
+ gr.File(label="Upload PDF", file_types=[".pdf"]),
39
+ gr.Textbox(label="Question", placeholder="Ask about the document...")
40
+ ],
41
+ outputs=[
42
+ gr.Textbox(label="Status"),
43
+ gr.Textbox(label="Answer", lines=5),
44
+ gr.Textbox(label="Sources", lines=2),
45
+ gr.Textbox(label="Evaluation", lines=3)
46
+ ],
47
+ title="Tech Explainer — RAG with Automatic Evaluation",
48
+ description="Upload a PDF and ask questions about it",
49
+ theme=gr.themes.Soft()
50
+ )
51
+
52
+ demo.launch(share=False, server_name="0.0.0.0", server_port=7860)