Files changed (1) hide show
  1. app.py +66 -0
app.py ADDED
@@ -0,0 +1,66 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import google.generativeai as genai
3
+ from PyPDF2 import PdfReader
4
+ from paddleocr import PaddleOCR
5
+ import os
6
+
7
+ # Step 1: Gemini API Key (must be set in Hugging Face Secrets)
8
+ genai.configure(api_key=os.getenv("GEMINI_API_KEY"))
9
+ model = genai.GenerativeModel('gemini-pro')
10
+
11
+ # Step 2: OCR Setup
12
+ ocr_model = PaddleOCR(use_angle_cls=True, lang='en')
13
+ documents = []
14
+
15
+ def extract_text(file):
16
+ ext = os.path.splitext(file.name)[1].lower()
17
+ text = ""
18
+ if ext == ".pdf":
19
+ reader = PdfReader(file)
20
+ for page in reader.pages:
21
+ text += page.extract_text() or ""
22
+ elif ext in [".jpg", ".jpeg", ".png"]:
23
+ result = ocr_model.ocr(file.name)
24
+ text = " ".join([line[1][0] for line in result[0]])
25
+ return text
26
+
27
+ def process_files(files):
28
+ global documents
29
+ documents = []
30
+ for f in files:
31
+ text = extract_text(f)
32
+ documents.append({"filename": f.name, "text": text})
33
+ return f"{len(files)} files processed and stored."
34
+
35
+ def answer_query(query):
36
+ if not documents:
37
+ return "Please upload and process files first."
38
+
39
+ prompt = "You are a research assistant. Analyze the following documents and answer the query.\n"
40
+ for i, doc in enumerate(documents):
41
+ prompt += f"\nDocument {i+1} ({doc['filename']}):\n{doc['text'][:2000]}\n"
42
+ prompt += f"\n\nQuestion: {query}\nAnswer with key themes and cite document numbers."
43
+
44
+ response = model.generate_content(prompt)
45
+ return response.text
46
+
47
+ # Step 3: Gradio Interface
48
+ with gr.Blocks() as demo:
49
+ gr.Markdown("# 📄 Gemini Document Research & Theme Identification Chatbot")
50
+
51
+ with gr.Row():
52
+ file_input = gr.File(file_types=[".pdf", ".jpg", ".png"], file_count="multiple", label="Upload Documents")
53
+ process_btn = gr.Button("Process Documents")
54
+
55
+ process_output = gr.Textbox(label="Processing Status")
56
+
57
+ with gr.Row():
58
+ query_input = gr.Textbox(label="Ask a question")
59
+ query_btn = gr.Button("Get Answer")
60
+
61
+ answer_output = gr.Textbox(label="Answer with Themes and Citations", lines=10)
62
+
63
+ process_btn.click(fn=process_files, inputs=[file_input], outputs=[process_output])
64
+ query_btn.click(fn=answer_query, inputs=[query_input], outputs=[answer_output])
65
+
66
+ demo.launch()