Upload app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import os
|
| 3 |
+
from rag import generate_chat_answer, conversation # Import updated functions
|
| 4 |
+
import logging
|
| 5 |
+
|
| 6 |
+
logging.basicConfig(level=logging.INFO)
|
| 7 |
+
logger = logging.getLogger(__name__)
|
| 8 |
+
|
| 9 |
+
def clear_chat():
|
| 10 |
+
conversation.clear_history()
|
| 11 |
+
return "", []
|
| 12 |
+
|
| 13 |
+
# Define custom CSS
|
| 14 |
+
custom_css = """
|
| 15 |
+
.gradio-container {
|
| 16 |
+
background: linear-gradient(rgba(255, 255, 255, 0.9), rgba(255, 255, 255, 0.9)),
|
| 17 |
+
url('file:///D:/Huawei NTI Ai Tr aining/Projecttt/Background.png');
|
| 18 |
+
background-size: cover;
|
| 19 |
+
background-position: center;
|
| 20 |
+
background-repeat: no-repeat;
|
| 21 |
+
}
|
| 22 |
+
|
| 23 |
+
/* Add rounded corners and shadow to the main container */
|
| 24 |
+
#container {
|
| 25 |
+
background: white;
|
| 26 |
+
border-radius: 15px;
|
| 27 |
+
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
|
| 28 |
+
padding: 20px;
|
| 29 |
+
margin: 20px;
|
| 30 |
+
max-width: 800px;
|
| 31 |
+
margin-left: auto;
|
| 32 |
+
margin-right: auto;
|
| 33 |
+
}
|
| 34 |
+
|
| 35 |
+
/* Style the header */
|
| 36 |
+
#header {
|
| 37 |
+
background: #4CAF50;
|
| 38 |
+
padding: 20px;
|
| 39 |
+
border-radius: 10px;
|
| 40 |
+
margin-bottom: 20px;
|
| 41 |
+
text-align: center;
|
| 42 |
+
color: white;
|
| 43 |
+
}
|
| 44 |
+
|
| 45 |
+
/* Style the buttons */
|
| 46 |
+
#buttons-section {
|
| 47 |
+
display: flex;
|
| 48 |
+
gap: 10px;
|
| 49 |
+
justify-content: center;
|
| 50 |
+
margin-top: 20px;
|
| 51 |
+
}
|
| 52 |
+
|
| 53 |
+
#submit-btn {
|
| 54 |
+
background: #4CAF50 !important;
|
| 55 |
+
color: white !important;
|
| 56 |
+
border: none !important;
|
| 57 |
+
border-radius: 5px !important;
|
| 58 |
+
padding: 10px 20px !important;
|
| 59 |
+
min-width: 120px;
|
| 60 |
+
}
|
| 61 |
+
|
| 62 |
+
#clear-btn {
|
| 63 |
+
background: white !important;
|
| 64 |
+
color: #4CAF50 !important;
|
| 65 |
+
border: 1px solid #4CAF50 !important;
|
| 66 |
+
border-radius: 5px !important;
|
| 67 |
+
padding: 10px 20px !important;
|
| 68 |
+
min-width: 120px;
|
| 69 |
+
}
|
| 70 |
+
"""
|
| 71 |
+
|
| 72 |
+
# Create the Gradio interface
|
| 73 |
+
with gr.Blocks(css=custom_css) as interface:
|
| 74 |
+
with gr.Column(elem_id="container"):
|
| 75 |
+
with gr.Column(elem_id="header"):
|
| 76 |
+
gr.Markdown("""
|
| 77 |
+
# 🌿 Health & Nutrition Assistant
|
| 78 |
+
### Your AI-powered healthcare knowledge companion
|
| 79 |
+
""")
|
| 80 |
+
|
| 81 |
+
chatbot = gr.Chatbot(label="Chat History", elem_id="chatbot", type="messages")
|
| 82 |
+
query = gr.Textbox(
|
| 83 |
+
label="Ask me anything about health and nutrition",
|
| 84 |
+
placeholder="Example: What are the best sources of plant-based protein?",
|
| 85 |
+
lines=3,
|
| 86 |
+
elem_id="query-input"
|
| 87 |
+
)
|
| 88 |
+
|
| 89 |
+
with gr.Row(elem_id="buttons-section"):
|
| 90 |
+
submit_btn = gr.Button("Send", elem_id="submit-btn")
|
| 91 |
+
clear_btn = gr.Button("Clear Chat", elem_id="clear-btn")
|
| 92 |
+
|
| 93 |
+
# Connect the buttons to your RAG functions
|
| 94 |
+
def respond(query, chat_history):
|
| 95 |
+
if not query.strip():
|
| 96 |
+
return "Please enter a valid question.", chat_history
|
| 97 |
+
|
| 98 |
+
answer = generate_chat_answer(query)
|
| 99 |
+
chat_history.append({"role": "user", "content": query})
|
| 100 |
+
chat_history.append({"role": "assistant", "content": answer})
|
| 101 |
+
logger.info(f"Updated Chat History:\n{chat_history}") # Log the updated chat history
|
| 102 |
+
return "", chat_history
|
| 103 |
+
|
| 104 |
+
# Update the button click
|
| 105 |
+
submit_btn.click(fn=respond, inputs=[query, chatbot], outputs=[query, chatbot])
|
| 106 |
+
clear_btn.click(fn=clear_chat, outputs=[query, chatbot])
|
| 107 |
+
|
| 108 |
+
# Launch the interface
|
| 109 |
+
if __name__ == "__main__":
|
| 110 |
+
interface.launch(share=True)
|