-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
82 lines (62 loc) · 2.63 KB
/
Copy pathapp.py
File metadata and controls
82 lines (62 loc) · 2.63 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
import gradio as gr
from dotenv import load_dotenv
from implementation.answer import answer_question
load_dotenv(override=True)
def format_context(context):
result = "<h2 style='color: #ff7800;'>Relevant Context</h2>\n\n"
for doc in context:
result += f"<span style='color: #ff7800;'>Source: {doc.metadata['source']}</span>\n\n"
result += doc.page_content + "\n\n"
return result
def chat(history):
if not history:
return history, "*No context retrieved*"
# Clean history dictionary payload to avoid sending raw JSON structures to the model
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]
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):
return "", history + [{"role": "user", "content": message}]
theme = gr.themes.Soft(font=["Inter", "system-ui", "sans-serif"])
# 1. Removed title/theme from Blocks constructor to align with Gradio 6 requirements
with gr.Blocks() as ui:
gr.Markdown("# 🏢 Insurellm Expert Assistant\nAsk me anything about Insurellm!")
with gr.Row():
with gr.Column(scale=1):
# 2. Removed 'show_copy_button' to fix the TypeError
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(
label="📚 Retrieved Context",
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])
# 3. Passed title and theme parameter directly into launch() method
ui.launch(inbrowser=True, theme=theme)
if __name__ == "__main__":
main()