Saraay commited on
Commit
d463bc2
·
verified ·
1 Parent(s): b265f5b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +111 -110
app.py CHANGED
@@ -1,110 +1,111 @@
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)
 
 
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
+
37
+ #header {
38
+ background: #4CAF50;
39
+ padding: 20px;
40
+ border-radius: 10px;
41
+ margin-bottom: 20px;
42
+ text-align: center;
43
+ color: white;
44
+ }
45
+
46
+ /* Style the buttons */
47
+ #buttons-section {
48
+ display: flex;
49
+ gap: 10px;
50
+ justify-content: center;
51
+ margin-top: 20px;
52
+ }
53
+
54
+ #submit-btn {
55
+ background: #4CAF50 !important;
56
+ color: white !important;
57
+ border: none !important;
58
+ border-radius: 5px !important;
59
+ padding: 10px 20px !important;
60
+ min-width: 120px;
61
+ }
62
+
63
+ #clear-btn {
64
+ background: white !important;
65
+ color: #4CAF50 !important;
66
+ border: 1px solid #4CAF50 !important;
67
+ border-radius: 5px !important;
68
+ padding: 10px 20px !important;
69
+ min-width: 120px;
70
+ }
71
+ """
72
+
73
+ # Create the Gradio interface
74
+ with gr.Blocks(css=custom_css) as interface:
75
+ with gr.Column(elem_id="container"):
76
+ with gr.Column(elem_id="header"):
77
+ gr.Markdown("""
78
+ # 🌿 Health & Nutrition Assistant
79
+ ### Your AI-powered healthcare knowledge companion
80
+ """)
81
+
82
+ chatbot = gr.Chatbot(label="Chat History", elem_id="chatbot", type="messages")
83
+ query = gr.Textbox(
84
+ label="Ask me anything about health and nutrition",
85
+ placeholder="Example: What are the best sources of plant-based protein?",
86
+ lines=3,
87
+ elem_id="query-input"
88
+ )
89
+
90
+ with gr.Row(elem_id="buttons-section"):
91
+ submit_btn = gr.Button("Send", elem_id="submit-btn")
92
+ clear_btn = gr.Button("Clear Chat", elem_id="clear-btn")
93
+
94
+ # Connect the buttons to your RAG functions
95
+ def respond(query, chat_history):
96
+ if not query.strip():
97
+ return "Please enter a valid question.", chat_history
98
+
99
+ answer = generate_chat_answer(query)
100
+ chat_history.append({"role": "user", "content": query})
101
+ chat_history.append({"role": "assistant", "content": answer})
102
+ logger.info(f"Updated Chat History:\n{chat_history}") # Log the updated chat history
103
+ return "", chat_history
104
+
105
+ # Update the button click
106
+ submit_btn.click(fn=respond, inputs=[query, chatbot], outputs=[query, chatbot])
107
+ clear_btn.click(fn=clear_chat, outputs=[query, chatbot])
108
+
109
+ # Launch the interface
110
+ if __name__ == "__main__":
111
+ interface.launch()