Mahmoud199717's picture
Upload folder using huggingface_hub
a71f62a verified
Raw
History Blame Contribute Delete
2.87 kB
import gradio as gr
from dotenv import load_dotenv
from answer import answer_question
load_dotenv(override=True)
# -----------------------------
# FORMAT CONTEXT PANEL
# -----------------------------
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" &nbsp;|&nbsp; <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
# -----------------------------
# CHAT LOGIC
# -----------------------------
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)
# -----------------------------
# UI
# -----------------------------
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, # πŸ‘ˆ renders newlines and markdown
)
message = gr.Textbox(
placeholder="Ask anything about Infor LN development...",
show_label=False,
submit_btn=True, # shows send button on textbox
)
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()