gmustafa413 commited on
Commit
24ea78c
·
verified ·
1 Parent(s): 4520352

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +94 -1
app.py CHANGED
@@ -1,6 +1,99 @@
1
  # Install dependencies
2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
  import gradio as gr
5
  import fitz
6
  import numpy as np
@@ -234,4 +327,4 @@ with gr.Blocks(title="RAG System") as app:
234
  )
235
 
236
  app.launch(share=True, debug=True)
237
-
 
1
  # Install dependencies
2
 
3
+ import gradio as gr
4
+ import fitz
5
+ import numpy as np
6
+ import requests
7
+ import faiss
8
+ import re
9
+ import json
10
+ import pandas as pd
11
+ from docx import Document
12
+ from pptx import Presentation
13
+ from sentence_transformers import SentenceTransformer
14
+ from concurrent.futures import ThreadPoolExecutor
15
+
16
+ # Configuration
17
+ GEMINI_API_KEY = "AIzaSyAPF8eVHU2jRWrQfwD8J9HPz4DrfIWK4GQ" # 🔑 REPLACE WITH YOUR GEMINI KEY
18
+ MODEL_NAME = "all-MiniLM-L6-v2"
19
+ CHUNK_SIZE = 1024
20
+ MAX_TOKENS = 4096
21
+ MODEL = SentenceTransformer(MODEL_NAME)
22
+ WORKERS = 8
23
 
24
+ class DocumentProcessor:
25
+ def __init__(self):
26
+ self.index = faiss.IndexFlatIP(MODEL.get_sentence_embedding_dimension())
27
+ self.chunks = []
28
+ self.processor_pool = ThreadPoolExecutor(max_workers=WORKERS)
29
+
30
+ # ... (keep all existing document processing methods unchanged) ...
31
+
32
+ def query(self, question):
33
+ if not self.chunks:
34
+ return "Please process documents first", False
35
+
36
+ try:
37
+ print("\n" + "="*40 + " QUERY PROCESSING " + "="*40)
38
+ print(f"Question: {question}")
39
+
40
+ # Generate embedding for the question
41
+ question_embedding = MODEL.encode([question], convert_to_tensor=True).cpu().numpy().astype('float32')
42
+
43
+ # Search FAISS index
44
+ _, indices = self.index.search(question_embedding, 3)
45
+ print(f"Top indices: {indices}")
46
+
47
+ # Get context from top chunks
48
+ context = "\n".join([self.chunks[i] for i in indices[0] if i < len(self.chunks)])
49
+ print(f"Context length: {len(context)} characters")
50
+
51
+ # Gemini API Call
52
+ url = f"https://generativelanguage.googleapis.com/v1beta/models/gemini-pro:generateContent?key={GEMINI_API_KEY}"
53
+ headers = {"Content-Type": "application/json"}
54
+
55
+ payload = {
56
+ "contents": [{
57
+ "parts": [{
58
+ "text": f"Answer concisely based on this context: {context}\n\nQuestion: {question}"
59
+ }]
60
+ }],
61
+ "generationConfig": {
62
+ "temperature": 0.3,
63
+ "maxOutputTokens": MAX_TOKENS
64
+ }
65
+ }
66
+
67
+ response = requests.post(
68
+ url,
69
+ headers=headers,
70
+ json=payload,
71
+ timeout=20
72
+ )
73
+
74
+ print(f"API Status Code: {response.status_code}")
75
+
76
+ if response.status_code != 200:
77
+ return f"API Error: {response.text}", False
78
+
79
+ # Parse Gemini response
80
+ response_json = response.json()
81
+ try:
82
+ final_answer = response_json['candidates'][0]['content']['parts'][0]['text']
83
+ except (KeyError, IndexError) as e:
84
+ print(f"Response parsing error: {str(e)}")
85
+ return "Error: Could not parse API response", False
86
+
87
+ print(f"Final Answer: {final_answer}")
88
+ return final_answer, True
89
+
90
+ except Exception as e:
91
+ print(f"Query Error: {str(e)}")
92
+ return f"Error: {str(e)}", False
93
+
94
+ # ... (keep the rest of the Gradio interface code unchanged) ...
95
+
96
+ '''
97
  import gradio as gr
98
  import fitz
99
  import numpy as np
 
327
  )
328
 
329
  app.launch(share=True, debug=True)
330
+ '''