-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp_advanced.py
More file actions
86 lines (66 loc) · 2.97 KB
/
Copy pathapp_advanced.py
File metadata and controls
86 lines (66 loc) · 2.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
import gradio as gr
from dotenv import load_dotenv
from implementation.answer_advanced import answer_question
load_dotenv(override=True)
def format_context(context):
if not context:
return "<h2 style='color: #ff7800;'>📚 Retrieved Context</h2>\n\n*No context retrieved*"
result = "<h2 style='color: #ff7800;'>📚 Retrieved Context (Advanced Reranked)</h2>\n\n"
for i, doc in enumerate(context, 1):
source = doc.metadata.get('source', 'unknown')
result += f"<div style='border-left: 3px solid #ff7800; padding-left: 10px; margin-bottom: 15px;'>"
result += f"<strong style='color: #ff7800;'>📄 Document {i}:</strong> "
result += f"<span style='color: #666;'>Source: {source}</span><br>\n"
result += f"<div style='margin-top: 5px; font-size: 0.95em;'>{doc.page_content}</div>\n"
result += f"</div>\n\n"
return result
def chat(history):
if not history:
return history, "*No context retrieved*"
# Strict structural data sanitation for modern Gradio versions
cleaned_history = []
for entry in history:
content = entry["content"]
if isinstance(content, dict):
text_content = content.get("text", "")
else:
text_content = str(content)
cleaned_history.append({
"role": entry["role"],
"content": text_content
})
last_message = cleaned_history[-1]["content"]
prior = cleaned_history[:-1]
# Process through Advanced RAG Pipeline
answer, context = answer_question(last_message, prior)
history.append({"role": "assistant", "content": answer})
return history, format_context(context)
def main():
def put_message_in_chatbot(message, history):
if not message or message.strip() == "":
return "", history
return "", history + [{"role": "user", "content": message}]
theme = gr.themes.Soft(font=["Inter", "system-ui", "sans-serif"])
with gr.Blocks() as ui:
gr.Markdown("# 🏢 Insurellm Expert Assistant (Advanced RAG)")
gr.Markdown("Leveraging Query-Rewriting, Multi-Query expansion, and local LLM Reranking.")
with gr.Row():
with gr.Column(scale=1):
chatbot = gr.Chatbot(label="💬 Conversation", height=600)
message = gr.Textbox(
label="Your Question",
placeholder="Ask anything about Insurellm...",
show_label=False,
)
with gr.Column(scale=1):
context_markdown = gr.Markdown(
value="*Retrieved context will appear here*",
container=True,
height=600,
)
message.submit(
put_message_in_chatbot, inputs=[message, chatbot], outputs=[message, chatbot]
).then(chat, inputs=chatbot, outputs=[chatbot, context_markdown])
ui.launch(inbrowser=True, theme=theme)
if __name__ == "__main__":
main()