martina3435 commited on
Commit
b0d23ed
·
verified ·
1 Parent(s): 82d991c

Update app.py

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