martina3435 commited on
Commit
b8a404b
·
verified ·
1 Parent(s): 1666b2a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +95 -31
app.py CHANGED
@@ -1,26 +1,57 @@
1
  import gradio as gr
2
- import os
3
- from rag import generate_chat_answer, conversation
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
  custom_css = """
14
  #container {
15
  background: white;
16
  border-radius: 15px;
17
  box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
18
  padding: 20px;
19
- margin: 20px;
20
- max-width: 800px;
21
- margin-left: auto;
22
- margin-right: auto;
23
  }
 
24
  #header {
25
  background: #4CAF50;
26
  padding: 20px;
@@ -29,61 +60,94 @@ custom_css = """
29
  text-align: center;
30
  color: white;
31
  }
 
32
  #buttons-section {
33
  display: flex;
34
  gap: 10px;
35
  justify-content: center;
36
  margin-top: 20px;
37
  }
 
38
  #submit-btn {
39
  background: #4CAF50 !important;
40
  color: white !important;
41
  border: none !important;
42
- border-radius: 5px !important;
43
  padding: 10px 20px !important;
44
  min-width: 120px;
45
  }
 
46
  #clear-btn {
47
  background: white !important;
48
  color: #4CAF50 !important;
49
  border: 1px solid #4CAF50 !important;
50
- border-radius: 5px !important;
51
  padding: 10px 20px !important;
52
  min-width: 120px;
53
  }
54
  """
55
 
56
- with gr.Blocks() as interface:
 
 
57
  with gr.Column(elem_id="container"):
 
58
  with gr.Column(elem_id="header"):
59
  gr.Markdown("""
60
- # 🌿 Health & Nutrition Assistant
61
- ### Your AI-powered healthcare knowledge companion
62
- """)
 
 
 
 
 
 
 
 
 
63
 
64
- chatbot = gr.Chatbot(label="Chat History", elem_id="chatbot")
65
  query = gr.Textbox(
66
- label="Ask me anything about health and nutrition",
67
- placeholder="Example: What are the best sources of plant-based protein?",
68
- lines=3,
69
- elem_id="query-input"
70
  )
71
 
72
  with gr.Row(elem_id="buttons-section"):
73
- submit_btn = gr.Button("Send", elem_id="submit-btn")
74
- clear_btn = gr.Button("Clear Chat", elem_id="clear-btn")
75
 
76
- def respond(query, chat_history):
77
- if not query.strip():
78
- return "Please enter a valid question.", chat_history
79
- answer = generate_chat_answer(query)
80
- chat_history.append({"role": "user", "content": query})
81
- chat_history.append({"role": "assistant", "content": answer})
82
- logger.info(f"Updated Chat History:\n{chat_history}")
83
- return "", chat_history
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
84
 
85
- submit_btn.click(fn=respond, inputs=[query, chatbot], outputs=[query, chatbot])
86
- clear_btn.click(fn=clear_chat, outputs=[query, chatbot])
87
 
 
 
 
88
  if __name__ == "__main__":
89
- interface.launch(css=custom_css)
 
1
  import gradio as gr
 
 
2
  import logging
3
+ from rag import generate_chat_answer, conversation
4
 
5
  logging.basicConfig(level=logging.INFO)
6
  logger = logging.getLogger(__name__)
7
 
8
+
9
+ # =====================================================
10
+ # Clear Chat
11
+ # =====================================================
12
  def clear_chat():
13
  conversation.clear_history()
14
+ return []
15
+
16
+
17
+ # =====================================================
18
+ # Respond Function
19
+ # =====================================================
20
+ def respond(query, chat_history):
21
+
22
+ if not query.strip():
23
+ return "", chat_history
24
+
25
+ answer = generate_chat_answer(query)
26
 
27
+ chat_history.append({
28
+ "role": "user",
29
+ "content": query
30
+ })
31
+
32
+ chat_history.append({
33
+ "role": "assistant",
34
+ "content": answer
35
+ })
36
+
37
+ logger.info(f"Chat History Updated:\n{chat_history}")
38
+
39
+ return "", chat_history
40
+
41
+
42
+ # =====================================================
43
+ # UI
44
+ # =====================================================
45
  custom_css = """
46
  #container {
47
  background: white;
48
  border-radius: 15px;
49
  box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
50
  padding: 20px;
51
+ margin: 20px auto;
52
+ max-width: 850px;
 
 
53
  }
54
+
55
  #header {
56
  background: #4CAF50;
57
  padding: 20px;
 
60
  text-align: center;
61
  color: white;
62
  }
63
+
64
  #buttons-section {
65
  display: flex;
66
  gap: 10px;
67
  justify-content: center;
68
  margin-top: 20px;
69
  }
70
+
71
  #submit-btn {
72
  background: #4CAF50 !important;
73
  color: white !important;
74
  border: none !important;
75
+ border-radius: 6px !important;
76
  padding: 10px 20px !important;
77
  min-width: 120px;
78
  }
79
+
80
  #clear-btn {
81
  background: white !important;
82
  color: #4CAF50 !important;
83
  border: 1px solid #4CAF50 !important;
84
+ border-radius: 6px !important;
85
  padding: 10px 20px !important;
86
  min-width: 120px;
87
  }
88
  """
89
 
90
+
91
+ with gr.Blocks(css=custom_css) as interface:
92
+
93
  with gr.Column(elem_id="container"):
94
+
95
  with gr.Column(elem_id="header"):
96
  gr.Markdown("""
97
+ # 🌿 Health & Nutrition Assistant
98
+ ### AI-powered medical & nutrition chatbot
99
+ """)
100
+
101
+ # =================================================
102
+ # Chatbot (NEW FORMAT)
103
+ # =================================================
104
+ chatbot = gr.Chatbot(
105
+ label="Chat History",
106
+ type="messages",
107
+ height=500
108
+ )
109
 
 
110
  query = gr.Textbox(
111
+ label="Ask your question",
112
+ placeholder="Example: What are protein-rich foods?",
113
+ lines=3
 
114
  )
115
 
116
  with gr.Row(elem_id="buttons-section"):
 
 
117
 
118
+ submit_btn = gr.Button(
119
+ "Send",
120
+ elem_id="submit-btn"
121
+ )
122
+
123
+ clear_btn = gr.Button(
124
+ "Clear Chat",
125
+ elem_id="clear-btn"
126
+ )
127
+
128
+ # =================================================
129
+ # Events
130
+ # =================================================
131
+ submit_btn.click(
132
+ fn=respond,
133
+ inputs=[query, chatbot],
134
+ outputs=[query, chatbot]
135
+ )
136
+
137
+ query.submit(
138
+ fn=respond,
139
+ inputs=[query, chatbot],
140
+ outputs=[query, chatbot]
141
+ )
142
+
143
+ clear_btn.click(
144
+ fn=clear_chat,
145
+ outputs=[chatbot]
146
+ )
147
 
 
 
148
 
149
+ # =====================================================
150
+ # Launch
151
+ # =====================================================
152
  if __name__ == "__main__":
153
+ interface.launch()