Files
LLM_Engineering_OLD/week5/app.py
2025-11-04 07:26:42 -05:00

62 lines
2.0 KiB
Python

import gradio as gr
from dotenv import load_dotenv
from pro_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):
last_message = history[-1]["content"]
prior = 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"])
with gr.Blocks(title="Insurellm Expert Assistant", theme=theme) as ui:
gr.Markdown("# 🏢 Insurellm Expert Assistant\nAsk me anything about Insurellm!")
with gr.Row():
with gr.Column(scale=1):
chatbot = gr.Chatbot(
label="💬 Conversation", height=600, type="messages", show_copy_button=True
)
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])
ui.launch(inbrowser=True)
if __name__ == "__main__":
main()