Spaces:
Runtime error
Runtime error
Download app.py from mohit-qlens/TP: direct link, hf CLI and curl.
- Browser
- Download file 2.1 kB
-
https://huggingface.co/spaces/mohit-qlens/TP/resolve/main/app.py
- Command line
-
hf download hf://spaces/mohit-qlens/TP/app.py
-
curl -L -o app.py https://huggingface.co/spaces/mohit-qlens/TP/resolve/main/app.py
2.1 kB
| 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: | |
| source = doc.metadata.get("source", doc.metadata.get("doc_type", "Unknown source")) | |
| result += f"<span style='color: #ff7800;'>Source: {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.Monochrome(font=["Inter", "system-ui", "sans-serif"]) | |
| with gr.Blocks(title="UG TP Expert Assistant", theme=theme) as ui: | |
| gr.Markdown("# 🏢 UG TP Expert Assistant\nAsk me anything about transfer pricing!") | |
| 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 transfer pricing...", | |
| 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() | |