Update app.py
Browse files
app.py
CHANGED
@@ -1,283 +1,288 @@
|
|
1 |
-
import os
|
2 |
-
import re
|
3 |
-
import asyncio
|
4 |
-
import gradio as gr
|
5 |
-
import RAG_Domain_know_doc
|
6 |
-
from web_search import search_autism
|
7 |
-
from RAG import rag_autism
|
8 |
-
from openai import OpenAI # Corrected import
|
9 |
-
from dotenv import load_dotenv
|
10 |
-
import Old_Document
|
11 |
-
import User_Specific_Documents
|
12 |
-
from prompt_template import (
|
13 |
-
Prompt_template_translation,
|
14 |
-
Prompt_template_LLM_Generation,
|
15 |
-
Prompt_template_Reranker,
|
16 |
-
Prompt_template_Wisal,
|
17 |
-
Prompt_template_Halluciations,
|
18 |
-
Prompt_template_paraphrasing,
|
19 |
-
Prompt_template_Translate_to_original,
|
20 |
-
Prompt_template_relevance,
|
21 |
-
Prompt_template_User_document_prompt
|
22 |
-
)
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
# Step
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
)
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
process_log.append(f"
|
123 |
-
|
124 |
-
#
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
)
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
)
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
)
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
)
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
)
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
-
)
|
178 |
-
|
179 |
-
|
180 |
-
|
181 |
-
|
182 |
-
|
183 |
-
|
184 |
-
|
185 |
-
|
186 |
-
|
187 |
-
|
188 |
-
|
189 |
-
|
190 |
-
|
191 |
-
|
192 |
-
|
193 |
-
|
194 |
-
|
195 |
-
|
196 |
-
|
197 |
-
|
198 |
-
|
199 |
-
|
200 |
-
|
201 |
-
|
202 |
-
|
203 |
-
|
204 |
-
|
205 |
-
|
206 |
-
|
207 |
-
|
208 |
-
|
209 |
-
|
210 |
-
|
211 |
-
|
212 |
-
|
213 |
-
|
214 |
-
|
215 |
-
|
216 |
-
|
217 |
-
|
218 |
-
|
219 |
-
|
220 |
-
|
221 |
-
|
222 |
-
|
223 |
-
|
224 |
-
|
225 |
-
|
226 |
-
|
227 |
-
|
228 |
-
|
229 |
-
|
230 |
-
|
231 |
-
|
232 |
-
|
233 |
-
|
234 |
-
|
235 |
-
|
236 |
-
|
237 |
-
|
238 |
-
|
239 |
-
|
240 |
-
|
241 |
-
|
242 |
-
|
243 |
-
|
244 |
-
|
245 |
-
|
246 |
-
|
247 |
-
|
248 |
-
|
249 |
-
#
|
250 |
-
|
251 |
-
|
252 |
-
|
253 |
-
|
254 |
-
|
255 |
-
|
256 |
-
|
257 |
-
|
258 |
-
|
259 |
-
|
260 |
-
|
261 |
-
|
262 |
-
|
263 |
-
|
264 |
-
|
265 |
-
|
266 |
-
|
267 |
-
|
268 |
-
|
269 |
-
|
270 |
-
|
271 |
-
|
272 |
-
|
273 |
-
|
274 |
-
|
275 |
-
|
276 |
-
|
277 |
-
|
278 |
-
|
279 |
-
|
280 |
-
|
281 |
-
|
282 |
-
|
283 |
-
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import re
|
3 |
+
import asyncio
|
4 |
+
import gradio as gr
|
5 |
+
import RAG_Domain_know_doc
|
6 |
+
from web_search import search_autism
|
7 |
+
from RAG import rag_autism
|
8 |
+
from openai import OpenAI # Corrected import
|
9 |
+
from dotenv import load_dotenv
|
10 |
+
import Old_Document
|
11 |
+
import User_Specific_Documents
|
12 |
+
from prompt_template import (
|
13 |
+
Prompt_template_translation,
|
14 |
+
Prompt_template_LLM_Generation,
|
15 |
+
Prompt_template_Reranker,
|
16 |
+
Prompt_template_Wisal,
|
17 |
+
Prompt_template_Halluciations,
|
18 |
+
Prompt_template_paraphrasing,
|
19 |
+
Prompt_template_Translate_to_original,
|
20 |
+
Prompt_template_relevance,
|
21 |
+
Prompt_template_User_document_prompt
|
22 |
+
)
|
23 |
+
|
24 |
+
GEMINI_API_KEY="AIzaSyCUCivstFpC9pq_jMHMYdlPrmh9Bx97dFo"
|
25 |
+
TAVILY_API_KEY="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"
|
26 |
+
OPENAI_API_KEY="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"
|
27 |
+
QDRANT_API_KEY="eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhY2Nlc3MiOiJtIiwiZXhwIjoxNzUxMDUxNzg4fQ.I9J-K7OM0BtcNKgj2d4uVM8QYAHYfFCVAyP4rlZkK2E"
|
28 |
+
QDRANT_URL="https://6a3aade6-e8ad-4a6c-a579-21f5af90b7e8.us-east4-0.gcp.cloud.qdrant.io"
|
29 |
+
OPENAI_API_KEY="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"
|
30 |
+
WEAVIATE_URL="https://xbvlj5rpqyiswspww0tthq.c0.us-west3.gcp.weaviate.cloud"
|
31 |
+
WEAVIATE_API_KEY="RU9acU1CYnNRTjY1S1ZFc18zNS9tQktaWlcwTzFEUjlscEVCUGF4YU5xRWx2MDhmTUtIdUhnOWdOTGVZPV92MjAw"
|
32 |
+
DEEPINFRA_API_KEY="285LUJulGIprqT6hcPhiXtcrphU04FG4"
|
33 |
+
DEEPINFRA_BASE_URL="https://api.deepinfra.com/v1/openai"
|
34 |
+
# Initialize OpenAI client
|
35 |
+
env = os.getenv("ENVIRONMENT", "production")
|
36 |
+
openai = OpenAI(
|
37 |
+
api_key=DEEPINFRA_TOKEN,
|
38 |
+
base_url="https://api.deepinfra.com/v1/openai",
|
39 |
+
)
|
40 |
+
|
41 |
+
|
42 |
+
|
43 |
+
# Rest of your code remains unchanged
|
44 |
+
# Helper to call chat completion synchronously
|
45 |
+
def call_llm(model: str, messages: list[dict], temperature: float = 0.0, **kwargs) -> str:
|
46 |
+
resp = openai.chat.completions.create(
|
47 |
+
model=model,
|
48 |
+
messages=messages,
|
49 |
+
temperature=temperature,
|
50 |
+
**kwargs
|
51 |
+
)
|
52 |
+
return resp.choices[0].message.content.strip()
|
53 |
+
|
54 |
+
# Basic greeting detection
|
55 |
+
def is_greeting(text: str) -> bool:
|
56 |
+
return bool(re.search(r"\b(hi|hello|hey|good (morning|afternoon|evening))\b", text, re.I))
|
57 |
+
|
58 |
+
|
59 |
+
def process_query(query: str, first_turn: bool = False):
|
60 |
+
intro = ""
|
61 |
+
process_log = []
|
62 |
+
|
63 |
+
if first_turn and (not query or query.strip() == ""):
|
64 |
+
intro = "Hello! I’m Wisal, an AI assistant developed by Compumacy AI, specializing in Autism Spectrum Disorders. How can I help you today?"
|
65 |
+
process_log.append(intro)
|
66 |
+
_save_process_log(process_log)
|
67 |
+
return intro
|
68 |
+
|
69 |
+
# ✅ Handle Yes/No replies
|
70 |
+
if query.strip().lower() == "no":
|
71 |
+
no_reply = (
|
72 |
+
"Hello, I’m Wisal, an AI assistant developed by Compumacy AI, "
|
73 |
+
"and a knowledgeable Autism specialist.\n"
|
74 |
+
"If you have any question related to autism, please submit a question specifically about autism."
|
75 |
+
)
|
76 |
+
process_log.append(f"User replied 'No'.\n{no_reply}")
|
77 |
+
_save_process_log(process_log)
|
78 |
+
return no_reply
|
79 |
+
elif query.strip().lower() == "yes":
|
80 |
+
process_log.append("User replied 'Yes'. Continuing system as normal.")
|
81 |
+
|
82 |
+
|
83 |
+
# 0: Handle simple greetings
|
84 |
+
if is_greeting(query):
|
85 |
+
greeting = intro + "Hello! I’m Wisal, your AI assistant developed by Compumacy AI. How can I help you today?"
|
86 |
+
process_log.append(f"Greeting detected.\n{greeting}")
|
87 |
+
_save_process_log(process_log)
|
88 |
+
return greeting
|
89 |
+
|
90 |
+
# 1: Translation & Rephrasing
|
91 |
+
corrected_query = call_llm(
|
92 |
+
model="Qwen/Qwen3-32B",
|
93 |
+
messages=[{"role": "user", "content": Prompt_template_translation.format(query=query)}],
|
94 |
+
reasoning_effort="none"
|
95 |
+
)
|
96 |
+
process_log.append(f"Corrected Query: {corrected_query}")
|
97 |
+
|
98 |
+
# 2: Relevance Check
|
99 |
+
relevance = call_llm(
|
100 |
+
model="Qwen/Qwen3-32B",
|
101 |
+
messages=[{"role": "user", "content": Prompt_template_relevance.format(corrected_query=corrected_query)}],
|
102 |
+
reasoning_effort="none"
|
103 |
+
)
|
104 |
+
process_log.append(f"Relevance: {relevance}")
|
105 |
+
if relevance != "RELATED":
|
106 |
+
process_log.append(f"Query not related. Returning: {relevance}")
|
107 |
+
_save_process_log(process_log)
|
108 |
+
return intro + relevance
|
109 |
+
|
110 |
+
# Step 3: Web Search
|
111 |
+
web_search_resp = asyncio.run(search_autism(corrected_query))
|
112 |
+
web_answer = web_search_resp.get("answer", "")
|
113 |
+
process_log.append(f"Web Search Answer: {web_answer}")
|
114 |
+
|
115 |
+
# Step 4: LLM Generation
|
116 |
+
gen_prompt = Prompt_template_LLM_Generation.format(new_query=corrected_query)
|
117 |
+
generated = call_llm(
|
118 |
+
model="Qwen/Qwen3-32B",
|
119 |
+
messages=[{"role": "user", "content": gen_prompt}],
|
120 |
+
reasoning_effort="none"
|
121 |
+
)
|
122 |
+
process_log.append(f"LLM Generated: {generated}")
|
123 |
+
|
124 |
+
# Step 5: RAG
|
125 |
+
rag_resp = asyncio.run(rag_autism(corrected_query, top_k=3))
|
126 |
+
rag_contexts = rag_resp.get("answer", [])
|
127 |
+
process_log.append(f"RAG Contexts: {rag_contexts}")
|
128 |
+
|
129 |
+
# 6) Reranking (now across 3 candidates)
|
130 |
+
rag_text = "\n".join(f"[{i+1}] {c}" for i, c in enumerate(rag_contexts))
|
131 |
+
answers_list = f"[1] {generated}\n[2] {web_answer}\n{rag_text}"
|
132 |
+
rerank_prompt = Prompt_template_Reranker.format(
|
133 |
+
new_query=corrected_query,
|
134 |
+
answers_list=answers_list
|
135 |
+
)
|
136 |
+
reranked = call_llm(
|
137 |
+
model="Qwen/Qwen3-32B",
|
138 |
+
messages=[{"role":"user","content":rerank_prompt}],
|
139 |
+
reasoning_effort="none"
|
140 |
+
)
|
141 |
+
process_log.append(f"Reranked: {reranked}")
|
142 |
+
|
143 |
+
# 7) Wisal final‐answer generation
|
144 |
+
wisal_prompt = Prompt_template_Wisal.format(
|
145 |
+
new_query=corrected_query,
|
146 |
+
document=reranked # use reranked output here
|
147 |
+
)
|
148 |
+
wisal = call_llm(
|
149 |
+
model="Qwen/Qwen3-32B",
|
150 |
+
messages=[{"role":"user","content":wisal_prompt}],
|
151 |
+
reasoning_effort="none"
|
152 |
+
)
|
153 |
+
process_log.append(f"Wisal Final Answer: {wisal}")
|
154 |
+
|
155 |
+
# 8) Hallucination Check
|
156 |
+
halluc_prompt = Prompt_template_Halluciations.format(
|
157 |
+
new_query=corrected_query,
|
158 |
+
answer=wisal,
|
159 |
+
document=generated
|
160 |
+
)
|
161 |
+
halluc = call_llm(
|
162 |
+
model="Qwen/Qwen3-32B",
|
163 |
+
messages=[{"role": "user", "content": halluc_prompt}],
|
164 |
+
reasoning_effort="none"
|
165 |
+
)
|
166 |
+
process_log.append(f"Hallucination Check: {halluc}")
|
167 |
+
score = int(halluc.split("Score: ")[1]) if "Score: " in halluc else 3
|
168 |
+
|
169 |
+
# 9) Paraphrase if needed
|
170 |
+
if score in (2, 3):
|
171 |
+
paraphrase = call_llm(
|
172 |
+
model="Qwen/Qwen3-32B",
|
173 |
+
messages=[{"role": "user", "content": Prompt_template_paraphrasing.format(document=generated)}],
|
174 |
+
reasoning_effort="none"
|
175 |
+
)
|
176 |
+
process_log.append(f"Paraphrased: {paraphrase}")
|
177 |
+
context_prompt = Prompt_template_Wisal.format(new_query=corrected_query, document=paraphrase)
|
178 |
+
final_doc = call_llm(
|
179 |
+
model="Qwen/Qwen3-32B",
|
180 |
+
messages=[{"role": "user", "content": context_prompt}],
|
181 |
+
reasoning_effort="none"
|
182 |
+
)
|
183 |
+
process_log.append(f"Wisal with Paraphrase: {final_doc}")
|
184 |
+
else:
|
185 |
+
final_doc = wisal
|
186 |
+
|
187 |
+
# 10) Translate back if needed (improved: only if input is not English)
|
188 |
+
import langdetect
|
189 |
+
try:
|
190 |
+
detected_lang = langdetect.detect(query)
|
191 |
+
except Exception:
|
192 |
+
detected_lang = "en"
|
193 |
+
if detected_lang != "en":
|
194 |
+
result = call_llm(
|
195 |
+
model="Qwen/Qwen3-32B",
|
196 |
+
messages=[{"role": "user", "content": Prompt_template_Translate_to_original.format(query=query, document=final_doc)}],
|
197 |
+
reasoning_effort="none"
|
198 |
+
)
|
199 |
+
process_log.append(f"Translated Back: {result}")
|
200 |
+
else:
|
201 |
+
result = final_doc
|
202 |
+
process_log.append(f"Final Result: {result}")
|
203 |
+
|
204 |
+
_save_process_log(process_log)
|
205 |
+
return intro + result
|
206 |
+
# Utility to save process log to a txt file
|
207 |
+
def _save_process_log(log_lines, filename="process_output.txt"):
|
208 |
+
import datetime
|
209 |
+
import os
|
210 |
+
# Ensure logs directory exists
|
211 |
+
logs_dir = os.path.join(os.path.dirname(__file__), "logs")
|
212 |
+
os.makedirs(logs_dir, exist_ok=True)
|
213 |
+
# Unique filename per question (timestamped)
|
214 |
+
timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S_%f")
|
215 |
+
log_filename = os.path.join(logs_dir, f"log_{timestamp}.txt")
|
216 |
+
try:
|
217 |
+
with open(log_filename, "w", encoding="utf-8") as f:
|
218 |
+
for line in log_lines:
|
219 |
+
f.write(str(line) + "\n\n")
|
220 |
+
except Exception as e:
|
221 |
+
pass
|
222 |
+
|
223 |
+
|
224 |
+
# Gradio UI for main pipeline, RAG_Domain_know_doc, and User_Specific_Documents , Old_Document
|
225 |
+
def main_pipeline_interface(query):
|
226 |
+
return process_query(query, first_turn=True)
|
227 |
+
|
228 |
+
|
229 |
+
def main_pipeline_with_doc(query, doc_file, doc_type):
|
230 |
+
# If no document, use main pipeline
|
231 |
+
if doc_file is None or doc_type == "None":
|
232 |
+
return process_query(query, first_turn=True)
|
233 |
+
|
234 |
+
safe_filename = os.path.basename(getattr(doc_file, 'name', str(doc_file)))
|
235 |
+
upload_dir = os.path.join(os.path.dirname(__file__), "uploaded_docs")
|
236 |
+
os.makedirs(upload_dir, exist_ok=True)
|
237 |
+
|
238 |
+
save_path = os.path.join(upload_dir, safe_filename)
|
239 |
+
|
240 |
+
# 💡 Check if doc_file is file-like (has `.read()`) or path-like (str or NamedString)
|
241 |
+
if hasattr(doc_file, 'read'):
|
242 |
+
# File-like object
|
243 |
+
file_bytes = doc_file.read()
|
244 |
+
else:
|
245 |
+
# It's a path (NamedString), read from file path
|
246 |
+
with open(str(doc_file), 'rb') as f:
|
247 |
+
file_bytes = f.read()
|
248 |
+
|
249 |
+
# Save the file content
|
250 |
+
with open(save_path, "wb") as f:
|
251 |
+
f.write(file_bytes)
|
252 |
+
|
253 |
+
|
254 |
+
# Route to correct document handler
|
255 |
+
if doc_type == "Knowledge Document":
|
256 |
+
status = RAG_Domain_know_doc.ingest_file(save_path)
|
257 |
+
answer = RAG_Domain_know_doc.answer_question(query)
|
258 |
+
return f"[Knowledge Document Uploaded]\n{status}\n\n{answer}"
|
259 |
+
elif doc_type == "User-Specific Document":
|
260 |
+
status = User_Specific_Documents.ingest_file(save_path)
|
261 |
+
answer = User_Specific_Documents.answer_question(query)
|
262 |
+
return f"[User-Specific Document Uploaded]\n{status}\n\n{answer}"
|
263 |
+
elif doc_type == "Old Document":
|
264 |
+
status = Old_Document.ingest_file(save_path)
|
265 |
+
answer = Old_Document.answer_question(query)
|
266 |
+
return f"[Old Document Uploaded]\n{status}\n\n{answer}"
|
267 |
+
else:
|
268 |
+
return "Invalid document type."
|
269 |
+
|
270 |
+
with gr.Blocks(title="Wisal Main Pipeline & RAG") as demo:
|
271 |
+
gr.Markdown("## Wisal: Autism AI Assistant (Main Pipeline)")
|
272 |
+
with gr.Tab("Main Pipeline"):
|
273 |
+
q = gr.Textbox(placeholder="Your question...", lines=2, label="Ask Wisal")
|
274 |
+
doc_file = gr.File(label="Optional: Upload Document (PDF, DOCX, TXT)")
|
275 |
+
doc_type = gr.Radio(["None", "Knowledge Document", "User-Specific Document", "Old Document"], value="None", label="Document Type")
|
276 |
+
btn = gr.Button("Submit")
|
277 |
+
out = gr.Textbox(label="Wisal Answer", lines=8, interactive=False)
|
278 |
+
btn.click(fn=main_pipeline_with_doc, inputs=[q, doc_file, doc_type], outputs=out)
|
279 |
+
with gr.Tab("Domain Knowledge RAG"):
|
280 |
+
RAG_Domain_know_doc.demo.render()
|
281 |
+
with gr.Tab("User-Specific Documents"):
|
282 |
+
User_Specific_Documents.demo.render()
|
283 |
+
with gr.Tab("Old Documents"):
|
284 |
+
Old_Document.demo.render()
|
285 |
+
|
286 |
+
if __name__ == "__main__":
|
287 |
+
demo.launch(debug=True)
|
288 |
+
|