Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,601 +1,634 @@
|
|
1 |
-
import os
|
2 |
-
import time
|
3 |
-
import gradio as gr
|
4 |
-
import uvicorn
|
5 |
-
from fastapi import FastAPI, HTTPException, Depends, File, UploadFile
|
6 |
-
from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
|
7 |
-
from pydantic import BaseModel
|
8 |
-
from typing import Optional, Dict, Any
|
9 |
-
import threading
|
10 |
-
import logging
|
11 |
-
from
|
12 |
-
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
13 |
-
from
|
14 |
-
from langchain.chains import RetrievalQA
|
15 |
-
from langchain.prompts import PromptTemplate
|
16 |
-
from langchain.callbacks.base import BaseCallbackHandler
|
17 |
-
from langchain_google_genai import ChatGoogleGenerativeAI, GoogleGenerativeAIEmbeddings
|
18 |
-
import tiktoken
|
19 |
-
|
20 |
-
# Configure logging
|
21 |
-
logging.basicConfig(level=logging.INFO)
|
22 |
-
logger = logging.getLogger(__name__)
|
23 |
-
|
24 |
-
# --- Configuration ---
|
25 |
-
CHUNK_SIZE = 800
|
26 |
-
CHUNK_OVERLAP = 100
|
27 |
-
MAX_TOKENS = 512
|
28 |
-
TEMPERATURE = 0.5
|
29 |
-
RETRIEVAL_K = 5
|
30 |
-
|
31 |
-
# --- Token Counting Setup ---
|
32 |
-
try:
|
33 |
-
tokenizer = tiktoken.get_encoding("cl100k_base")
|
34 |
-
except:
|
35 |
-
print("Tiktoken encoder 'cl100k_base' not found. Using basic split().")
|
36 |
-
tokenizer = type('obj', (object,), {'encode': lambda x: x.split()})()
|
37 |
-
|
38 |
-
def estimate_tokens(text):
|
39 |
-
"""Estimates token count for a given text."""
|
40 |
-
return len(tokenizer.encode(text))
|
41 |
-
|
42 |
-
# Custom Callback Handler to track LLM token usage
|
43 |
-
class TokenUsageCallbackHandler(BaseCallbackHandler):
|
44 |
-
"""Callback handler to track token usage in LLM calls."""
|
45 |
-
def __init__(self):
|
46 |
-
super().__init__()
|
47 |
-
self.reset_counters()
|
48 |
-
|
49 |
-
def reset_counters(self):
|
50 |
-
self.total_prompt_tokens = 0
|
51 |
-
self.total_completion_tokens = 0
|
52 |
-
self.total_llm_calls = 0
|
53 |
-
|
54 |
-
def on_llm_end(self, response, **kwargs):
|
55 |
-
"""Collect token usage from the LLM response."""
|
56 |
-
self.total_llm_calls += 1
|
57 |
-
llm_output = response.llm_output
|
58 |
-
|
59 |
-
if llm_output and 'usage_metadata' in llm_output:
|
60 |
-
usage = llm_output['usage_metadata']
|
61 |
-
prompt_tokens = usage.get('prompt_token_count', 0)
|
62 |
-
completion_tokens = usage.get('candidates_token_count', 0)
|
63 |
-
|
64 |
-
self.total_prompt_tokens += prompt_tokens
|
65 |
-
self.total_completion_tokens += completion_tokens
|
66 |
-
|
67 |
-
def get_total_tokens(self):
|
68 |
-
"""Returns the total prompt and completion tokens."""
|
69 |
-
return {
|
70 |
-
"total_prompt_tokens": self.total_prompt_tokens,
|
71 |
-
"total_completion_tokens": self.total_completion_tokens,
|
72 |
-
"total_llm_tokens": self.total_prompt_tokens + self.total_completion_tokens,
|
73 |
-
"total_llm_calls": self.total_llm_calls
|
74 |
-
}
|
75 |
-
|
76 |
-
# --- Pydantic Models for API ---
|
77 |
-
class InitializeRequest(BaseModel):
|
78 |
-
api_key: str
|
79 |
-
document_content: Optional[str] = None
|
80 |
-
|
81 |
-
class QueryRequest(BaseModel):
|
82 |
-
query: str
|
83 |
-
api_key: str
|
84 |
-
|
85 |
-
class InitializeResponse(BaseModel):
|
86 |
-
success: bool
|
87 |
-
message: str
|
88 |
-
chunks: Optional[int] = None
|
89 |
-
estimated_tokens: Optional[int] = None
|
90 |
-
|
91 |
-
class QueryResponse(BaseModel):
|
92 |
-
success: bool
|
93 |
-
answer: str
|
94 |
-
response_time: float
|
95 |
-
query_tokens: int
|
96 |
-
llm_tokens: Dict[str, int]
|
97 |
-
session_stats: Dict[str, int]
|
98 |
-
|
99 |
-
class StatsResponse(BaseModel):
|
100 |
-
total_queries: int
|
101 |
-
total_embedding_tokens: int
|
102 |
-
total_llm_tokens: int
|
103 |
-
total_llm_calls: int
|
104 |
-
initialization_complete: bool
|
105 |
-
|
106 |
-
# --- Global Variables ---
|
107 |
-
class RAGSystem:
|
108 |
-
def __init__(self):
|
109 |
-
self.vector_store = None
|
110 |
-
self.qa_chain = None
|
111 |
-
self.token_callback_handler = TokenUsageCallbackHandler()
|
112 |
-
self.session_stats = {
|
113 |
-
"total_queries": 0,
|
114 |
-
"total_embedding_tokens": 0,
|
115 |
-
"initialization_complete": False
|
116 |
-
}
|
117 |
-
self.current_api_key = None
|
118 |
-
|
119 |
-
# Global RAG system instance
|
120 |
-
rag_system = RAGSystem()
|
121 |
-
|
122 |
-
def initialize_rag_system(api_key, file_content=None):
|
123 |
-
"""Initialize the RAG system with API key and optional file content."""
|
124 |
-
global rag_system
|
125 |
-
|
126 |
-
try:
|
127 |
-
# Set API key
|
128 |
-
os.environ["GOOGLE_API_KEY"] = api_key
|
129 |
-
rag_system.current_api_key = api_key
|
130 |
-
|
131 |
-
# Initialize embeddings
|
132 |
-
embeddings = GoogleGenerativeAIEmbeddings(
|
133 |
-
model="models/embedding-001",
|
134 |
-
google_api_key=api_key
|
135 |
-
)
|
136 |
-
|
137 |
-
# Initialize LLM
|
138 |
-
llm = ChatGoogleGenerativeAI(
|
139 |
-
model="gemini-1.5-flash",
|
140 |
-
google_api_key=api_key,
|
141 |
-
temperature=TEMPERATURE,
|
142 |
-
max_tokens=MAX_TOKENS,
|
143 |
-
callbacks=[rag_system.token_callback_handler],
|
144 |
-
verbose=False
|
145 |
-
)
|
146 |
-
|
147 |
-
# Load or use default document
|
148 |
-
if file_content:
|
149 |
-
# Save uploaded file content
|
150 |
-
with open("uploaded_document.txt", "w", encoding="utf-8") as f:
|
151 |
-
f.write(file_content)
|
152 |
-
loader = TextLoader("uploaded_document.txt")
|
153 |
-
else:
|
154 |
-
# Check if default maize_data.txt exists
|
155 |
-
if os.path.exists("maize_data.txt"):
|
156 |
-
loader = TextLoader("maize_data.txt")
|
157 |
-
else:
|
158 |
-
return "β No document found. Please upload a file or ensure maize_data.txt exists."
|
159 |
-
|
160 |
-
# Load and split documents
|
161 |
-
documents = loader.load()
|
162 |
-
text_splitter = RecursiveCharacterTextSplitter(
|
163 |
-
chunk_size=CHUNK_SIZE,
|
164 |
-
chunk_overlap=CHUNK_OVERLAP
|
165 |
-
)
|
166 |
-
chunks = text_splitter.split_documents(documents)
|
167 |
-
|
168 |
-
# Estimate embedding tokens
|
169 |
-
initial_embedding_tokens = sum(estimate_tokens(chunk.page_content) for chunk in chunks)
|
170 |
-
rag_system.session_stats["total_embedding_tokens"] = initial_embedding_tokens
|
171 |
-
|
172 |
-
# Create vector store
|
173 |
-
rag_system.vector_store = FAISS.from_documents(chunks, embeddings)
|
174 |
-
|
175 |
-
# Create prompt template
|
176 |
-
prompt_template = PromptTemplate(
|
177 |
-
input_variables=["context", "question"],
|
178 |
-
template="""
|
179 |
-
You are an expert in maize agriculture. Use the following context ONLY to answer the question accurately and helpfully. If the context doesn't contain the answer, say "Based on the provided context, I cannot answer this question.".
|
180 |
-
|
181 |
-
Context:
|
182 |
-
{context}
|
183 |
-
|
184 |
-
Question: {question}
|
185 |
-
|
186 |
-
Answer:"""
|
187 |
-
)
|
188 |
-
|
189 |
-
# Set up QA chain
|
190 |
-
rag_system.qa_chain = RetrievalQA.from_chain_type(
|
191 |
-
llm=llm,
|
192 |
-
chain_type="stuff",
|
193 |
-
retriever=rag_system.vector_store.as_retriever(search_kwargs={"k": RETRIEVAL_K}),
|
194 |
-
chain_type_kwargs={"prompt": prompt_template},
|
195 |
-
callbacks=[rag_system.token_callback_handler],
|
196 |
-
return_source_documents=True
|
197 |
-
)
|
198 |
-
|
199 |
-
rag_system.session_stats["initialization_complete"] = True
|
200 |
-
|
201 |
-
return f"β
RAG system initialized successfully!\nπ Document processed: {len(chunks)} chunks\nπ’ Estimated embedding tokens: ~{initial_embedding_tokens}"
|
202 |
-
|
203 |
-
except Exception as e:
|
204 |
-
logger.error(f"Initialization failed: {str(e)}")
|
205 |
-
return f"β Initialization failed: {str(e)}"
|
206 |
-
|
207 |
-
def process_query(query, api_key):
|
208 |
-
"""Process a user query through the RAG system."""
|
209 |
-
global rag_system
|
210 |
-
|
211 |
-
if not api_key:
|
212 |
-
return "β Please provide a Google API key first.", ""
|
213 |
-
|
214 |
-
if not rag_system.qa_chain:
|
215 |
-
return "β RAG system not initialized. Please initialize first.", ""
|
216 |
-
|
217 |
-
if not query.strip():
|
218 |
-
return "β Please enter a question.", ""
|
219 |
-
|
220 |
-
try:
|
221 |
-
# Estimate query embedding tokens
|
222 |
-
query_tokens = estimate_tokens(query)
|
223 |
-
rag_system.session_stats["total_embedding_tokens"] += query_tokens
|
224 |
-
rag_system.session_stats["total_queries"] += 1
|
225 |
-
|
226 |
-
# Process query
|
227 |
-
start_time = time.time()
|
228 |
-
result = rag_system.qa_chain({"query": query})
|
229 |
-
end_time = time.time()
|
230 |
-
|
231 |
-
# Get token usage
|
232 |
-
llm_tokens = rag_system.token_callback_handler.get_total_tokens()
|
233 |
-
|
234 |
-
# Format response
|
235 |
-
answer = result['result']
|
236 |
-
|
237 |
-
# Create stats summary
|
238 |
-
stats = f"""
|
239 |
-
π **Query Statistics:**
|
240 |
-
- Response time: {end_time - start_time:.2f} seconds
|
241 |
-
- Query tokens (estimated): ~{query_tokens}
|
242 |
-
- LLM tokens (this query): Prompt: {llm_tokens['total_prompt_tokens']}, Completion: {llm_tokens['total_completion_tokens']}
|
243 |
-
|
244 |
-
π **Session Statistics:**
|
245 |
-
- Total queries: {rag_system.session_stats['total_queries']}
|
246 |
-
- Total embedding tokens: ~{rag_system.session_stats['total_embedding_tokens']}
|
247 |
-
- Total LLM calls: {llm_tokens['total_llm_calls']}
|
248 |
-
- Total LLM tokens: {llm_tokens['total_llm_tokens']}
|
249 |
-
"""
|
250 |
-
|
251 |
-
return answer, stats
|
252 |
-
|
253 |
-
except Exception as e:
|
254 |
-
logger.error(f"Error processing query: {str(e)}")
|
255 |
-
return f"β Error processing query: {str(e)}", ""
|
256 |
-
|
257 |
-
def upload_file_and_initialize(api_key, file):
|
258 |
-
"""Handle file upload and system initialization."""
|
259 |
-
if not api_key:
|
260 |
-
return "β Please provide a Google API key first."
|
261 |
-
|
262 |
-
if file is None:
|
263 |
-
return initialize_rag_system(api_key)
|
264 |
-
|
265 |
-
try:
|
266 |
-
#
|
267 |
-
|
268 |
-
|
269 |
-
|
270 |
-
|
271 |
-
|
272 |
-
|
273 |
-
|
274 |
-
|
275 |
-
|
276 |
-
|
277 |
-
|
278 |
-
|
279 |
-
|
280 |
-
|
281 |
-
|
282 |
-
|
283 |
-
|
284 |
-
|
285 |
-
|
286 |
-
|
287 |
-
|
288 |
-
)
|
289 |
-
|
290 |
-
|
291 |
-
|
292 |
-
|
293 |
-
|
294 |
-
|
295 |
-
|
296 |
-
|
297 |
-
|
298 |
-
|
299 |
-
|
300 |
-
|
301 |
-
|
302 |
-
|
303 |
-
"
|
304 |
-
|
305 |
-
|
306 |
-
|
307 |
-
|
308 |
-
|
309 |
-
|
310 |
-
|
311 |
-
|
312 |
-
|
313 |
-
|
314 |
-
|
315 |
-
|
316 |
-
|
317 |
-
|
318 |
-
|
319 |
-
|
320 |
-
|
321 |
-
|
322 |
-
|
323 |
-
|
324 |
-
|
325 |
-
|
326 |
-
|
327 |
-
|
328 |
-
|
329 |
-
|
330 |
-
|
331 |
-
|
332 |
-
|
333 |
-
|
334 |
-
|
335 |
-
|
336 |
-
|
337 |
-
|
338 |
-
|
339 |
-
|
340 |
-
|
341 |
-
|
342 |
-
|
343 |
-
|
344 |
-
|
345 |
-
|
346 |
-
|
347 |
-
|
348 |
-
|
349 |
-
|
350 |
-
|
351 |
-
|
352 |
-
|
353 |
-
|
354 |
-
|
355 |
-
|
356 |
-
|
357 |
-
|
358 |
-
|
359 |
-
|
360 |
-
|
361 |
-
|
362 |
-
|
363 |
-
|
364 |
-
|
365 |
-
|
366 |
-
|
367 |
-
|
368 |
-
|
369 |
-
|
370 |
-
|
371 |
-
|
372 |
-
|
373 |
-
|
374 |
-
|
375 |
-
|
376 |
-
|
377 |
-
|
378 |
-
|
379 |
-
|
380 |
-
|
381 |
-
|
382 |
-
|
383 |
-
|
384 |
-
|
385 |
-
|
386 |
-
|
387 |
-
|
388 |
-
|
389 |
-
|
390 |
-
|
391 |
-
|
392 |
-
|
393 |
-
|
394 |
-
|
395 |
-
|
396 |
-
|
397 |
-
|
398 |
-
|
399 |
-
|
400 |
-
|
401 |
-
|
402 |
-
|
403 |
-
|
404 |
-
|
405 |
-
|
406 |
-
|
407 |
-
|
408 |
-
|
409 |
-
|
410 |
-
|
411 |
-
|
412 |
-
|
413 |
-
|
414 |
-
|
415 |
-
|
416 |
-
|
417 |
-
|
418 |
-
|
419 |
-
|
420 |
-
|
421 |
-
|
422 |
-
|
423 |
-
|
424 |
-
|
425 |
-
|
426 |
-
|
427 |
-
|
428 |
-
|
429 |
-
|
430 |
-
|
431 |
-
|
432 |
-
|
433 |
-
|
434 |
-
|
435 |
-
|
436 |
-
|
437 |
-
|
438 |
-
|
439 |
-
|
440 |
-
|
441 |
-
|
442 |
-
|
443 |
-
|
444 |
-
|
445 |
-
|
446 |
-
|
447 |
-
|
448 |
-
|
449 |
-
|
450 |
-
|
451 |
-
|
452 |
-
|
453 |
-
|
454 |
-
|
455 |
-
|
456 |
-
|
457 |
-
|
458 |
-
|
459 |
-
|
460 |
-
|
461 |
-
|
462 |
-
|
463 |
-
|
464 |
-
|
465 |
-
|
466 |
-
|
467 |
-
|
468 |
-
|
469 |
-
|
470 |
-
|
471 |
-
|
472 |
-
|
473 |
-
|
474 |
-
|
475 |
-
|
476 |
-
|
477 |
-
|
478 |
-
|
479 |
-
|
480 |
-
|
481 |
-
|
482 |
-
|
483 |
-
|
484 |
-
|
485 |
-
|
486 |
-
|
487 |
-
|
488 |
-
|
489 |
-
|
490 |
-
with gr.Column(scale=
|
491 |
-
|
492 |
-
|
493 |
-
|
494 |
-
|
495 |
-
|
496 |
-
|
497 |
-
|
498 |
-
|
499 |
-
|
500 |
-
|
501 |
-
|
502 |
-
|
503 |
-
|
504 |
-
|
505 |
-
|
506 |
-
|
507 |
-
|
508 |
-
|
509 |
-
|
510 |
-
|
511 |
-
|
512 |
-
|
513 |
-
|
514 |
-
|
515 |
-
|
516 |
-
|
517 |
-
|
518 |
-
|
519 |
-
|
520 |
-
|
521 |
-
|
522 |
-
|
523 |
-
|
524 |
-
|
525 |
-
|
526 |
-
|
527 |
-
|
528 |
-
|
529 |
-
|
530 |
-
|
531 |
-
|
532 |
-
|
533 |
-
|
534 |
-
|
535 |
-
|
536 |
-
|
537 |
-
|
538 |
-
|
539 |
-
|
540 |
-
|
541 |
-
|
542 |
-
|
543 |
-
|
544 |
-
|
545 |
-
|
546 |
-
|
547 |
-
|
548 |
-
|
549 |
-
|
550 |
-
|
551 |
-
|
552 |
-
|
553 |
-
|
554 |
-
|
555 |
-
|
556 |
-
|
557 |
-
|
558 |
-
|
559 |
-
|
560 |
-
|
561 |
-
|
562 |
-
|
563 |
-
|
564 |
-
|
565 |
-
|
566 |
-
|
567 |
-
|
568 |
-
|
569 |
-
|
570 |
-
|
571 |
-
|
572 |
-
|
573 |
-
|
574 |
-
|
575 |
-
|
576 |
-
|
577 |
-
|
578 |
-
|
579 |
-
|
580 |
-
|
581 |
-
|
582 |
-
|
583 |
-
|
584 |
-
|
585 |
-
|
586 |
-
|
587 |
-
|
588 |
-
|
589 |
-
|
590 |
-
|
591 |
-
|
592 |
-
|
593 |
-
|
594 |
-
|
595 |
-
|
596 |
-
|
597 |
-
|
598 |
-
|
599 |
-
|
600 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
601 |
run_gradio()
|
|
|
1 |
+
import os
|
2 |
+
import time
|
3 |
+
import gradio as gr
|
4 |
+
import uvicorn
|
5 |
+
from fastapi import FastAPI, HTTPException, Depends, File, UploadFile
|
6 |
+
from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
|
7 |
+
from pydantic import BaseModel
|
8 |
+
from typing import Optional, Dict, Any
|
9 |
+
import threading
|
10 |
+
import logging
|
11 |
+
from langchain_community.document_loaders import TextLoader
|
12 |
+
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
13 |
+
from langchain_community.vectorstores import FAISS
|
14 |
+
from langchain.chains import RetrievalQA
|
15 |
+
from langchain.prompts import PromptTemplate
|
16 |
+
from langchain.callbacks.base import BaseCallbackHandler
|
17 |
+
from langchain_google_genai import ChatGoogleGenerativeAI, GoogleGenerativeAIEmbeddings
|
18 |
+
import tiktoken
|
19 |
+
|
20 |
+
# Configure logging
|
21 |
+
logging.basicConfig(level=logging.INFO)
|
22 |
+
logger = logging.getLogger(__name__)
|
23 |
+
|
24 |
+
# --- Configuration ---
|
25 |
+
CHUNK_SIZE = 800
|
26 |
+
CHUNK_OVERLAP = 100
|
27 |
+
MAX_TOKENS = 512
|
28 |
+
TEMPERATURE = 0.5
|
29 |
+
RETRIEVAL_K = 5
|
30 |
+
|
31 |
+
# --- Token Counting Setup ---
|
32 |
+
try:
|
33 |
+
tokenizer = tiktoken.get_encoding("cl100k_base")
|
34 |
+
except:
|
35 |
+
print("Tiktoken encoder 'cl100k_base' not found. Using basic split().")
|
36 |
+
tokenizer = type('obj', (object,), {'encode': lambda x: x.split()})()
|
37 |
+
|
38 |
+
def estimate_tokens(text):
|
39 |
+
"""Estimates token count for a given text."""
|
40 |
+
return len(tokenizer.encode(text))
|
41 |
+
|
42 |
+
# Custom Callback Handler to track LLM token usage
|
43 |
+
class TokenUsageCallbackHandler(BaseCallbackHandler):
|
44 |
+
"""Callback handler to track token usage in LLM calls."""
|
45 |
+
def __init__(self):
|
46 |
+
super().__init__()
|
47 |
+
self.reset_counters()
|
48 |
+
|
49 |
+
def reset_counters(self):
|
50 |
+
self.total_prompt_tokens = 0
|
51 |
+
self.total_completion_tokens = 0
|
52 |
+
self.total_llm_calls = 0
|
53 |
+
|
54 |
+
def on_llm_end(self, response, **kwargs):
|
55 |
+
"""Collect token usage from the LLM response."""
|
56 |
+
self.total_llm_calls += 1
|
57 |
+
llm_output = response.llm_output
|
58 |
+
|
59 |
+
if llm_output and 'usage_metadata' in llm_output:
|
60 |
+
usage = llm_output['usage_metadata']
|
61 |
+
prompt_tokens = usage.get('prompt_token_count', 0)
|
62 |
+
completion_tokens = usage.get('candidates_token_count', 0)
|
63 |
+
|
64 |
+
self.total_prompt_tokens += prompt_tokens
|
65 |
+
self.total_completion_tokens += completion_tokens
|
66 |
+
|
67 |
+
def get_total_tokens(self):
|
68 |
+
"""Returns the total prompt and completion tokens."""
|
69 |
+
return {
|
70 |
+
"total_prompt_tokens": self.total_prompt_tokens,
|
71 |
+
"total_completion_tokens": self.total_completion_tokens,
|
72 |
+
"total_llm_tokens": self.total_prompt_tokens + self.total_completion_tokens,
|
73 |
+
"total_llm_calls": self.total_llm_calls
|
74 |
+
}
|
75 |
+
|
76 |
+
# --- Pydantic Models for API ---
|
77 |
+
class InitializeRequest(BaseModel):
|
78 |
+
api_key: str
|
79 |
+
document_content: Optional[str] = None
|
80 |
+
|
81 |
+
class QueryRequest(BaseModel):
|
82 |
+
query: str
|
83 |
+
api_key: str
|
84 |
+
|
85 |
+
class InitializeResponse(BaseModel):
|
86 |
+
success: bool
|
87 |
+
message: str
|
88 |
+
chunks: Optional[int] = None
|
89 |
+
estimated_tokens: Optional[int] = None
|
90 |
+
|
91 |
+
class QueryResponse(BaseModel):
|
92 |
+
success: bool
|
93 |
+
answer: str
|
94 |
+
response_time: float
|
95 |
+
query_tokens: int
|
96 |
+
llm_tokens: Dict[str, int]
|
97 |
+
session_stats: Dict[str, int]
|
98 |
+
|
99 |
+
class StatsResponse(BaseModel):
|
100 |
+
total_queries: int
|
101 |
+
total_embedding_tokens: int
|
102 |
+
total_llm_tokens: int
|
103 |
+
total_llm_calls: int
|
104 |
+
initialization_complete: bool
|
105 |
+
|
106 |
+
# --- Global Variables ---
|
107 |
+
class RAGSystem:
|
108 |
+
def __init__(self):
|
109 |
+
self.vector_store = None
|
110 |
+
self.qa_chain = None
|
111 |
+
self.token_callback_handler = TokenUsageCallbackHandler()
|
112 |
+
self.session_stats = {
|
113 |
+
"total_queries": 0,
|
114 |
+
"total_embedding_tokens": 0,
|
115 |
+
"initialization_complete": False
|
116 |
+
}
|
117 |
+
self.current_api_key = None
|
118 |
+
|
119 |
+
# Global RAG system instance
|
120 |
+
rag_system = RAGSystem()
|
121 |
+
|
122 |
+
def initialize_rag_system(api_key, file_content=None):
|
123 |
+
"""Initialize the RAG system with API key and optional file content."""
|
124 |
+
global rag_system
|
125 |
+
|
126 |
+
try:
|
127 |
+
# Set API key
|
128 |
+
os.environ["GOOGLE_API_KEY"] = api_key
|
129 |
+
rag_system.current_api_key = api_key
|
130 |
+
|
131 |
+
# Initialize embeddings
|
132 |
+
embeddings = GoogleGenerativeAIEmbeddings(
|
133 |
+
model="models/embedding-001",
|
134 |
+
google_api_key=api_key
|
135 |
+
)
|
136 |
+
|
137 |
+
# Initialize LLM
|
138 |
+
llm = ChatGoogleGenerativeAI(
|
139 |
+
model="gemini-1.5-flash",
|
140 |
+
google_api_key=api_key,
|
141 |
+
temperature=TEMPERATURE,
|
142 |
+
max_tokens=MAX_TOKENS,
|
143 |
+
callbacks=[rag_system.token_callback_handler],
|
144 |
+
verbose=False
|
145 |
+
)
|
146 |
+
|
147 |
+
# Load or use default document
|
148 |
+
if file_content:
|
149 |
+
# Save uploaded file content
|
150 |
+
with open("uploaded_document.txt", "w", encoding="utf-8") as f:
|
151 |
+
f.write(file_content)
|
152 |
+
loader = TextLoader("uploaded_document.txt")
|
153 |
+
else:
|
154 |
+
# Check if default maize_data.txt exists
|
155 |
+
if os.path.exists("maize_data.txt"):
|
156 |
+
loader = TextLoader("maize_data.txt")
|
157 |
+
else:
|
158 |
+
return "β No document found. Please upload a file or ensure maize_data.txt exists."
|
159 |
+
|
160 |
+
# Load and split documents
|
161 |
+
documents = loader.load()
|
162 |
+
text_splitter = RecursiveCharacterTextSplitter(
|
163 |
+
chunk_size=CHUNK_SIZE,
|
164 |
+
chunk_overlap=CHUNK_OVERLAP
|
165 |
+
)
|
166 |
+
chunks = text_splitter.split_documents(documents)
|
167 |
+
|
168 |
+
# Estimate embedding tokens
|
169 |
+
initial_embedding_tokens = sum(estimate_tokens(chunk.page_content) for chunk in chunks)
|
170 |
+
rag_system.session_stats["total_embedding_tokens"] = initial_embedding_tokens
|
171 |
+
|
172 |
+
# Create vector store
|
173 |
+
rag_system.vector_store = FAISS.from_documents(chunks, embeddings)
|
174 |
+
|
175 |
+
# Create prompt template
|
176 |
+
prompt_template = PromptTemplate(
|
177 |
+
input_variables=["context", "question"],
|
178 |
+
template="""
|
179 |
+
You are an expert in maize agriculture. Use the following context ONLY to answer the question accurately and helpfully. If the context doesn't contain the answer, say "Based on the provided context, I cannot answer this question.".
|
180 |
+
|
181 |
+
Context:
|
182 |
+
{context}
|
183 |
+
|
184 |
+
Question: {question}
|
185 |
+
|
186 |
+
Answer:"""
|
187 |
+
)
|
188 |
+
|
189 |
+
# Set up QA chain
|
190 |
+
rag_system.qa_chain = RetrievalQA.from_chain_type(
|
191 |
+
llm=llm,
|
192 |
+
chain_type="stuff",
|
193 |
+
retriever=rag_system.vector_store.as_retriever(search_kwargs={"k": RETRIEVAL_K}),
|
194 |
+
chain_type_kwargs={"prompt": prompt_template},
|
195 |
+
callbacks=[rag_system.token_callback_handler],
|
196 |
+
return_source_documents=True
|
197 |
+
)
|
198 |
+
|
199 |
+
rag_system.session_stats["initialization_complete"] = True
|
200 |
+
|
201 |
+
return f"β
RAG system initialized successfully!\nπ Document processed: {len(chunks)} chunks\nπ’ Estimated embedding tokens: ~{initial_embedding_tokens}"
|
202 |
+
|
203 |
+
except Exception as e:
|
204 |
+
logger.error(f"Initialization failed: {str(e)}")
|
205 |
+
return f"β Initialization failed: {str(e)}"
|
206 |
+
|
207 |
+
def process_query(query, api_key):
|
208 |
+
"""Process a user query through the RAG system."""
|
209 |
+
global rag_system
|
210 |
+
|
211 |
+
if not api_key:
|
212 |
+
return "β Please provide a Google API key first.", ""
|
213 |
+
|
214 |
+
if not rag_system.qa_chain:
|
215 |
+
return "β RAG system not initialized. Please initialize first.", ""
|
216 |
+
|
217 |
+
if not query.strip():
|
218 |
+
return "β Please enter a question.", ""
|
219 |
+
|
220 |
+
try:
|
221 |
+
# Estimate query embedding tokens
|
222 |
+
query_tokens = estimate_tokens(query)
|
223 |
+
rag_system.session_stats["total_embedding_tokens"] += query_tokens
|
224 |
+
rag_system.session_stats["total_queries"] += 1
|
225 |
+
|
226 |
+
# Process query
|
227 |
+
start_time = time.time()
|
228 |
+
result = rag_system.qa_chain({"query": query})
|
229 |
+
end_time = time.time()
|
230 |
+
|
231 |
+
# Get token usage
|
232 |
+
llm_tokens = rag_system.token_callback_handler.get_total_tokens()
|
233 |
+
|
234 |
+
# Format response
|
235 |
+
answer = result['result']
|
236 |
+
|
237 |
+
# Create stats summary
|
238 |
+
stats = f"""
|
239 |
+
π **Query Statistics:**
|
240 |
+
- Response time: {end_time - start_time:.2f} seconds
|
241 |
+
- Query tokens (estimated): ~{query_tokens}
|
242 |
+
- LLM tokens (this query): Prompt: {llm_tokens['total_prompt_tokens']}, Completion: {llm_tokens['total_completion_tokens']}
|
243 |
+
|
244 |
+
π **Session Statistics:**
|
245 |
+
- Total queries: {rag_system.session_stats['total_queries']}
|
246 |
+
- Total embedding tokens: ~{rag_system.session_stats['total_embedding_tokens']}
|
247 |
+
- Total LLM calls: {llm_tokens['total_llm_calls']}
|
248 |
+
- Total LLM tokens: {llm_tokens['total_llm_tokens']}
|
249 |
+
"""
|
250 |
+
|
251 |
+
return answer, stats
|
252 |
+
|
253 |
+
except Exception as e:
|
254 |
+
logger.error(f"Error processing query: {str(e)}")
|
255 |
+
return f"β Error processing query: {str(e)}", ""
|
256 |
+
|
257 |
+
def upload_file_and_initialize(api_key, file):
|
258 |
+
"""Handle file upload and system initialization."""
|
259 |
+
if not api_key:
|
260 |
+
return "β Please provide a Google API key first."
|
261 |
+
|
262 |
+
if file is None:
|
263 |
+
return initialize_rag_system(api_key)
|
264 |
+
|
265 |
+
try:
|
266 |
+
# Handle different file object types based on Gradio version
|
267 |
+
if hasattr(file, 'name'):
|
268 |
+
# Newer Gradio versions - file has .name attribute
|
269 |
+
with open(file.name, 'r', encoding='utf-8') as f:
|
270 |
+
file_content = f.read()
|
271 |
+
elif isinstance(file, str):
|
272 |
+
# File path as string
|
273 |
+
with open(file, 'r', encoding='utf-8') as f:
|
274 |
+
file_content = f.read()
|
275 |
+
elif hasattr(file, 'read'):
|
276 |
+
# File-like object
|
277 |
+
file_content = file.read()
|
278 |
+
if isinstance(file_content, bytes):
|
279 |
+
file_content = file_content.decode('utf-8')
|
280 |
+
else:
|
281 |
+
# Fallback - try to read as bytes and decode
|
282 |
+
file_content = file.decode('utf-8') if isinstance(file, bytes) else str(file)
|
283 |
+
|
284 |
+
return initialize_rag_system(api_key, file_content)
|
285 |
+
|
286 |
+
except Exception as e:
|
287 |
+
logger.error(f"Error reading uploaded file: {str(e)}")
|
288 |
+
return f"β Error reading uploaded file: {str(e)}"
|
289 |
+
|
290 |
+
def reset_session():
|
291 |
+
"""Reset the session statistics."""
|
292 |
+
global rag_system
|
293 |
+
rag_system.token_callback_handler.reset_counters()
|
294 |
+
rag_system.session_stats = {
|
295 |
+
"total_queries": 0,
|
296 |
+
"total_embedding_tokens": 0,
|
297 |
+
"initialization_complete": False
|
298 |
+
}
|
299 |
+
return "π Session statistics reset."
|
300 |
+
|
301 |
+
# --- FastAPI Setup ---
|
302 |
+
app = FastAPI(
|
303 |
+
title="Maize RAG Q&A System API",
|
304 |
+
description="API for the Maize Agriculture RAG Q&A System",
|
305 |
+
version="1.0.0"
|
306 |
+
)
|
307 |
+
|
308 |
+
# Optional: Add API key authentication for API endpoints
|
309 |
+
security = HTTPBearer(auto_error=False)
|
310 |
+
|
311 |
+
async def get_api_key(credentials: HTTPAuthorizationCredentials = Depends(security)):
|
312 |
+
"""Extract API key from Authorization header (optional)"""
|
313 |
+
if credentials:
|
314 |
+
return credentials.credentials
|
315 |
+
return None
|
316 |
+
|
317 |
+
# --- API Endpoints ---
|
318 |
+
|
319 |
+
@app.get("/")
|
320 |
+
async def root():
|
321 |
+
"""Root endpoint"""
|
322 |
+
return {"message": "Maize RAG Q&A System API", "status": "running"}
|
323 |
+
|
324 |
+
@app.get("/health")
|
325 |
+
async def health_check():
|
326 |
+
"""Health check endpoint"""
|
327 |
+
return {
|
328 |
+
"status": "healthy",
|
329 |
+
"system_initialized": rag_system.session_stats["initialization_complete"]
|
330 |
+
}
|
331 |
+
|
332 |
+
@app.post("/initialize", response_model=InitializeResponse)
|
333 |
+
async def initialize_system(request: InitializeRequest):
|
334 |
+
"""Initialize the RAG system"""
|
335 |
+
try:
|
336 |
+
result = initialize_rag_system(request.api_key, request.document_content)
|
337 |
+
|
338 |
+
if "β
" in result:
|
339 |
+
# Parse successful result
|
340 |
+
lines = result.split('\n')
|
341 |
+
chunks = None
|
342 |
+
tokens = None
|
343 |
+
|
344 |
+
for line in lines:
|
345 |
+
if "chunks" in line:
|
346 |
+
chunks = int(line.split(': ')[1].split(' ')[0])
|
347 |
+
elif "tokens" in line:
|
348 |
+
tokens = int(line.split('~')[1])
|
349 |
+
|
350 |
+
return InitializeResponse(
|
351 |
+
success=True,
|
352 |
+
message=result,
|
353 |
+
chunks=chunks,
|
354 |
+
estimated_tokens=tokens
|
355 |
+
)
|
356 |
+
else:
|
357 |
+
return InitializeResponse(
|
358 |
+
success=False,
|
359 |
+
message=result
|
360 |
+
)
|
361 |
+
|
362 |
+
except Exception as e:
|
363 |
+
logger.error(f"API initialization error: {str(e)}")
|
364 |
+
raise HTTPException(status_code=500, detail=str(e))
|
365 |
+
|
366 |
+
@app.post("/query", response_model=QueryResponse)
|
367 |
+
async def query_system(request: QueryRequest):
|
368 |
+
"""Query the RAG system"""
|
369 |
+
try:
|
370 |
+
if not rag_system.session_stats["initialization_complete"]:
|
371 |
+
raise HTTPException(status_code=400, detail="System not initialized")
|
372 |
+
|
373 |
+
# Estimate query embedding tokens
|
374 |
+
query_tokens = estimate_tokens(request.query)
|
375 |
+
rag_system.session_stats["total_embedding_tokens"] += query_tokens
|
376 |
+
rag_system.session_stats["total_queries"] += 1
|
377 |
+
|
378 |
+
# Process query
|
379 |
+
start_time = time.time()
|
380 |
+
result = rag_system.qa_chain({"query": request.query})
|
381 |
+
end_time = time.time()
|
382 |
+
|
383 |
+
# Get token usage
|
384 |
+
llm_tokens = rag_system.token_callback_handler.get_total_tokens()
|
385 |
+
|
386 |
+
response_time = end_time - start_time
|
387 |
+
|
388 |
+
return QueryResponse(
|
389 |
+
success=True,
|
390 |
+
answer=result['result'],
|
391 |
+
response_time=response_time,
|
392 |
+
query_tokens=query_tokens,
|
393 |
+
llm_tokens=llm_tokens,
|
394 |
+
session_stats=rag_system.session_stats
|
395 |
+
)
|
396 |
+
|
397 |
+
except Exception as e:
|
398 |
+
logger.error(f"API query error: {str(e)}")
|
399 |
+
raise HTTPException(status_code=500, detail=str(e))
|
400 |
+
|
401 |
+
@app.get("/stats", response_model=StatsResponse)
|
402 |
+
async def get_stats():
|
403 |
+
"""Get current session statistics"""
|
404 |
+
llm_tokens = rag_system.token_callback_handler.get_total_tokens()
|
405 |
+
|
406 |
+
return StatsResponse(
|
407 |
+
total_queries=rag_system.session_stats["total_queries"],
|
408 |
+
total_embedding_tokens=rag_system.session_stats["total_embedding_tokens"],
|
409 |
+
total_llm_tokens=llm_tokens["total_llm_tokens"],
|
410 |
+
total_llm_calls=llm_tokens["total_llm_calls"],
|
411 |
+
initialization_complete=rag_system.session_stats["initialization_complete"]
|
412 |
+
)
|
413 |
+
|
414 |
+
@app.post("/reset")
|
415 |
+
async def reset_system():
|
416 |
+
"""Reset session statistics"""
|
417 |
+
reset_session()
|
418 |
+
return {"message": "Session reset successfully"}
|
419 |
+
|
420 |
+
@app.post("/upload-document")
|
421 |
+
async def upload_document(
|
422 |
+
file: UploadFile = File(...),
|
423 |
+
api_key: str = None
|
424 |
+
):
|
425 |
+
"""Upload a document and initialize the system"""
|
426 |
+
try:
|
427 |
+
if not api_key:
|
428 |
+
raise HTTPException(status_code=400, detail="API key required")
|
429 |
+
|
430 |
+
# Read uploaded file
|
431 |
+
content = await file.read()
|
432 |
+
file_content = content.decode('utf-8')
|
433 |
+
|
434 |
+
# Initialize system with uploaded content
|
435 |
+
result = initialize_rag_system(api_key, file_content)
|
436 |
+
|
437 |
+
if "β
" in result:
|
438 |
+
return {"success": True, "message": result}
|
439 |
+
else:
|
440 |
+
return {"success": False, "message": result}
|
441 |
+
|
442 |
+
except Exception as e:
|
443 |
+
logger.error(f"Document upload error: {str(e)}")
|
444 |
+
raise HTTPException(status_code=500, detail=str(e))
|
445 |
+
|
446 |
+
# Create Gradio interface with version compatibility
|
447 |
+
def create_interface():
|
448 |
+
# Check Gradio version for compatibility
|
449 |
+
import gradio as gr
|
450 |
+
gradio_version = gr.__version__
|
451 |
+
|
452 |
+
with gr.Blocks(title="Maize RAG Q&A System", theme=gr.themes.Soft()) as demo:
|
453 |
+
gr.Markdown("""
|
454 |
+
# π½ Maize Agriculture RAG Q&A System
|
455 |
+
|
456 |
+
This system uses Retrieval-Augmented Generation (RAG) to answer questions about maize agriculture.
|
457 |
+
Upload your own document or use the default maize dataset.
|
458 |
+
""")
|
459 |
+
|
460 |
+
with gr.Row():
|
461 |
+
with gr.Column(scale=2):
|
462 |
+
api_key_input = gr.Textbox(
|
463 |
+
label="π Google API Key",
|
464 |
+
placeholder="Enter your Google Generative AI API key",
|
465 |
+
type="password"
|
466 |
+
)
|
467 |
+
gr.Markdown("Get your API key from Google AI Studio")
|
468 |
+
|
469 |
+
with gr.Column(scale=1):
|
470 |
+
reset_btn = gr.Button("π Reset Session", variant="secondary")
|
471 |
+
|
472 |
+
with gr.Row():
|
473 |
+
with gr.Column():
|
474 |
+
file_upload = gr.File(
|
475 |
+
label="π Upload Document (Optional)",
|
476 |
+
file_types=[".txt"]
|
477 |
+
)
|
478 |
+
gr.Markdown("Upload a text file or use the default maize dataset")
|
479 |
+
|
480 |
+
init_btn = gr.Button("π Initialize RAG System", variant="primary")
|
481 |
+
init_output = gr.Textbox(
|
482 |
+
label="π Initialization Status",
|
483 |
+
lines=3,
|
484 |
+
interactive=False
|
485 |
+
)
|
486 |
+
|
487 |
+
gr.Markdown("## π¬ Ask Questions")
|
488 |
+
|
489 |
+
with gr.Row():
|
490 |
+
with gr.Column(scale=3):
|
491 |
+
query_input = gr.Textbox(
|
492 |
+
label="β Your Question",
|
493 |
+
placeholder="Ask something about maize agriculture...",
|
494 |
+
lines=2
|
495 |
+
)
|
496 |
+
|
497 |
+
# Sample questions
|
498 |
+
sample_questions = [
|
499 |
+
"What are the main pests affecting maize crops?",
|
500 |
+
"How should maize be irrigated?",
|
501 |
+
"What is the ideal soil type for maize?",
|
502 |
+
"What are the nutritional requirements of maize?",
|
503 |
+
"When is the best time to harvest maize?"
|
504 |
+
]
|
505 |
+
|
506 |
+
# Use Examples component if available, otherwise just show as markdown
|
507 |
+
try:
|
508 |
+
gr.Examples(
|
509 |
+
examples=sample_questions,
|
510 |
+
inputs=query_input,
|
511 |
+
label="π‘ Sample Questions"
|
512 |
+
)
|
513 |
+
except:
|
514 |
+
gr.Markdown("π‘ **Sample Questions:**\n" +
|
515 |
+
"\n".join([f"- {q}" for q in sample_questions]))
|
516 |
+
|
517 |
+
with gr.Column(scale=1):
|
518 |
+
submit_btn = gr.Button("π Ask", variant="primary")
|
519 |
+
|
520 |
+
with gr.Row():
|
521 |
+
with gr.Column(scale=2):
|
522 |
+
answer_output = gr.Textbox(
|
523 |
+
label="π€ Answer",
|
524 |
+
lines=6,
|
525 |
+
interactive=False
|
526 |
+
)
|
527 |
+
|
528 |
+
with gr.Column(scale=1):
|
529 |
+
stats_output = gr.Markdown(
|
530 |
+
value="π Statistics will appear here after queries."
|
531 |
+
)
|
532 |
+
|
533 |
+
# Event handlers
|
534 |
+
init_btn.click(
|
535 |
+
upload_file_and_initialize,
|
536 |
+
inputs=[api_key_input, file_upload],
|
537 |
+
outputs=init_output
|
538 |
+
)
|
539 |
+
|
540 |
+
submit_btn.click(
|
541 |
+
process_query,
|
542 |
+
inputs=[query_input, api_key_input],
|
543 |
+
outputs=[answer_output, stats_output]
|
544 |
+
)
|
545 |
+
|
546 |
+
query_input.submit(
|
547 |
+
process_query,
|
548 |
+
inputs=[query_input, api_key_input],
|
549 |
+
outputs=[answer_output, stats_output]
|
550 |
+
)
|
551 |
+
|
552 |
+
reset_btn.click(
|
553 |
+
reset_session,
|
554 |
+
outputs=init_output
|
555 |
+
)
|
556 |
+
|
557 |
+
gr.Markdown("""
|
558 |
+
## π Instructions:
|
559 |
+
1. **Enter your Google API Key** (required)
|
560 |
+
2. **Upload a document** (optional - uses default maize dataset if not provided)
|
561 |
+
3. **Initialize the RAG system** by clicking "Initialize RAG System"
|
562 |
+
4. **Ask questions** about the document content
|
563 |
+
5. **View statistics** to monitor token usage and costs
|
564 |
+
|
565 |
+
## π° Cost Information:
|
566 |
+
- **Gemini 1.5 Flash**: Input: $0.075/1M tokens, Output: $0.30/1M tokens
|
567 |
+
- **Embedding Model**: $0.025/1M tokens
|
568 |
+
|
569 |
+
Token usage is estimated and displayed for cost tracking.
|
570 |
+
|
571 |
+
## π API Access:
|
572 |
+
This system also provides REST API endpoints:
|
573 |
+
- **API Docs**: Add `/docs` to the URL for interactive API documentation
|
574 |
+
- **Health Check**: `GET /health`
|
575 |
+
- **Initialize**: `POST /initialize`
|
576 |
+
- **Query**: `POST /query`
|
577 |
+
""")
|
578 |
+
|
579 |
+
return demo
|
580 |
+
|
581 |
+
# Create and launch the interface
|
582 |
+
def run_gradio():
|
583 |
+
"""Run Gradio interface"""
|
584 |
+
demo = create_interface()
|
585 |
+
demo.launch(
|
586 |
+
server_name="0.0.0.0",
|
587 |
+
server_port=7860,
|
588 |
+
show_error=True,
|
589 |
+
quiet=True # Reduce Gradio logs in combined mode
|
590 |
+
)
|
591 |
+
|
592 |
+
def run_fastapi():
|
593 |
+
"""Run FastAPI server"""
|
594 |
+
uvicorn.run(
|
595 |
+
app,
|
596 |
+
host="0.0.0.0",
|
597 |
+
port=8000,
|
598 |
+
log_level="info"
|
599 |
+
)
|
600 |
+
|
601 |
+
if __name__ == "__main__":
|
602 |
+
import sys
|
603 |
+
|
604 |
+
if len(sys.argv) > 1:
|
605 |
+
mode = sys.argv[1]
|
606 |
+
|
607 |
+
if mode == "api":
|
608 |
+
# Run only FastAPI
|
609 |
+
print("Starting FastAPI server on port 8000...")
|
610 |
+
run_fastapi()
|
611 |
+
elif mode == "gradio":
|
612 |
+
# Run only Gradio
|
613 |
+
print("Starting Gradio interface on port 7860...")
|
614 |
+
run_gradio()
|
615 |
+
elif mode == "both":
|
616 |
+
# Run both servers
|
617 |
+
print("Starting both FastAPI (port 8000) and Gradio (port 7860)...")
|
618 |
+
|
619 |
+
# Start FastAPI in a separate thread
|
620 |
+
fastapi_thread = threading.Thread(target=run_fastapi)
|
621 |
+
fastapi_thread.daemon = True
|
622 |
+
fastapi_thread.start()
|
623 |
+
|
624 |
+
# Start Gradio in main thread
|
625 |
+
time.sleep(2) # Give FastAPI time to start
|
626 |
+
run_gradio()
|
627 |
+
else:
|
628 |
+
print("Usage: python app.py [api|gradio|both]")
|
629 |
+
print("Default: gradio only")
|
630 |
+
run_gradio()
|
631 |
+
else:
|
632 |
+
# Default: run only Gradio (for Hugging Face Spaces compatibility)
|
633 |
+
print("Starting Gradio interface on port 7860...")
|
634 |
run_gradio()
|