| import gradio as gr
|
| from dotenv import load_dotenv
|
| from answer import answer_question
|
|
|
| load_dotenv(override=True)
|
|
|
|
|
|
|
|
|
| def format_context(docs):
|
| result = "<h2 style='color: #ff7800;'>π Retrieved Context</h2>\n\n"
|
| for doc in docs:
|
| title = doc.metadata.get("title", "Unknown")
|
| doc_type = doc.metadata.get("doc_type", "")
|
| result += f"<hr/>"
|
| result += f"<p><strong style='color: #ff7800;'>π {title}</strong>"
|
| if doc_type:
|
| result += f" | <code>{doc_type}</code>"
|
| result += "</p>"
|
| result += f"<pre style='background:#f4f4f4; padding:10px; border-radius:6px; font-size:12px; white-space:pre-wrap;'>{doc.page_content}</pre>\n\n"
|
| return result
|
|
|
|
|
|
|
|
|
| def chat(history):
|
| last_message = history[-1]["content"]
|
| prior = history[:-1]
|
|
|
| answer, docs = answer_question(last_message, prior)
|
|
|
| history.append({"role": "assistant", "content": answer})
|
| return history, format_context(docs)
|
|
|
|
|
|
|
|
|
| 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="Infor LN Dev Assistant") as ui:
|
| gr.Markdown("# π οΈ Infor LN Development Assistant\nAsk me anything about 4GL, DAL, functions, and Infor LN development!")
|
|
|
| with gr.Row():
|
| with gr.Column(scale=1):
|
| chatbot = gr.Chatbot(
|
| label="π¬ Conversation",
|
| height=600,
|
| render_markdown=True,
|
| )
|
| message = gr.Textbox(
|
| placeholder="Ask anything about Infor LN development...",
|
| show_label=False,
|
| submit_btn=True,
|
| )
|
|
|
| with gr.Column(scale=1):
|
| context_display = gr.HTML(
|
| label="π Retrieved Context",
|
| value="<p style='color:gray;'>Retrieved context will appear here...</p>",
|
| )
|
|
|
| message.submit(
|
| put_message_in_chatbot,
|
| inputs=[message, chatbot],
|
| outputs=[message, chatbot]
|
| ).then(
|
| chat,
|
| inputs=chatbot,
|
| outputs=[chatbot, context_display]
|
| )
|
|
|
| ui.launch(
|
| server_name="0.0.0.0",
|
| server_port=7860,
|
| inbrowser=True,
|
| share=False,
|
| theme=theme
|
| )
|
|
|
| if __name__ == "__main__":
|
| main() |