mayedatariq commited on
Commit
dd61e60
·
verified ·
1 Parent(s): 5600599

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -80
app.py CHANGED
@@ -1,7 +1,6 @@
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
3
 
4
- # SPOTIFY EMBED
5
  spotify_embed = """
6
  <iframe data-testid="embed-iframe"
7
  style="border-radius:12px"
@@ -15,10 +14,8 @@ clipboard-write;
15
  encrypted-media;
16
  fullscreen;
17
  picture-in-picture"
18
- loading="lazy"></iframe>
19
- """
20
 
21
- # THEME
22
  theme = gr.themes.Soft().set(
23
 
24
  body_background_fill="#fff7fb",
@@ -45,16 +42,12 @@ theme = gr.themes.Soft().set(
45
  link_text_color="#c77dff"
46
  )
47
 
48
- # LOAD KNOWLEDGE BASE
49
- with open("knowledge.txt", "r", encoding="utf-8") as f:
50
  knowledge_base = f.read()
51
 
52
- # MODEL
53
  client = InferenceClient("Qwen/Qwen2.5-7B-Instruct")
54
 
55
- # SYSTEM PROMPTS
56
  SYSTEM_MESSAGES = {
57
-
58
  "wellness": (
59
  "You are a kind wellness chatbot. "
60
  "Give practical, supportive, and thoughtful advice "
@@ -69,18 +62,39 @@ SYSTEM_MESSAGES = {
69
  }
70
 
71
 
72
- # RESPONSE FUNCTION
73
  def respond(message, history, mode):
74
 
75
- # NO MODE SELECTED
76
  if mode is None:
77
- yield (
78
- "Please select a mode first using the buttons above.",
79
- mode
80
- )
81
- return
82
 
83
- # SYSTEM MESSAGE
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
84
  messages = [
85
  {
86
  "role": "system",
@@ -88,19 +102,16 @@ def respond(message, history, mode):
88
  }
89
  ]
90
 
91
- # HISTORY
92
  if history:
93
  messages.extend(history)
94
 
95
- # USER MESSAGE
96
  messages.append({
97
- "role": "user",
98
- "content": message
99
  })
100
-
101
  response = ""
102
 
103
- # STREAM RESPONSE
104
  for chunk in client.chat_completion(
105
  messages=messages,
106
  max_tokens=1024,
@@ -109,86 +120,36 @@ def respond(message, history, mode):
109
  stream=True,
110
  ):
111
 
112
- token = chunk.choices[0].delta.content or ""
113
 
114
  if token:
115
  response += token
116
  yield response, mode
117
 
118
 
119
- # UI
120
  with gr.Blocks(theme=theme) as demo:
121
 
122
- # MODE STATE
123
  mode_state = gr.State(None)
124
 
125
- # BANNER
126
- gr.Image(
127
- "Website Banner.png",
128
- show_label=False,
129
- container=False
130
- )
131
 
132
- # TITLE
133
  gr.Markdown("# ***Wellness and Storytelling ChatBot***")
134
 
135
  gr.Markdown(
136
- "DISCLAIMER: While signing, please spell out individual letters in ASL instead of the distinct signs.\n\n"
137
- "Choose a mode using the buttons below:"
 
 
138
  )
139
 
140
- # BUTTONS
141
- with gr.Row():
142
-
143
- wellness_btn = gr.Button(
144
- "🌿 Wellness",
145
- variant="primary"
146
- )
147
-
148
- story_btn = gr.Button(
149
- "📖 Story",
150
- variant="secondary"
151
- )
152
-
153
- # CHAT INTERFACE
154
  chatbot = gr.ChatInterface(
155
  fn=respond,
156
  additional_inputs=[mode_state],
157
- additional_outputs=[mode_state]
158
- )
159
-
160
-
161
- # BUTTON ACTIONS
162
- wellness_btn.click(
163
- fn=lambda: (
164
- [
165
- {
166
- "role": "assistant",
167
- "content": "🌿 Wellness mode activated!"
168
- }
169
- ],
170
- "wellness"
171
- ),
172
- outputs=[chatbot.chatbot, mode_state]
173
- )
174
-
175
- story_btn.click(
176
- fn=lambda: (
177
- [
178
- {
179
- "role": "assistant",
180
- "content": "📖 Story mode activated!"
181
- }
182
- ],
183
- "story"
184
- ),
185
- outputs=[chatbot.chatbot, mode_state]
186
  )
187
 
188
- # MUSIC SECTION
189
  gr.Markdown("### 🎵 Music We Listened To While Building This")
190
 
191
  gr.HTML(spotify_embed)
192
 
193
- # LAUNCH
194
  demo.launch(debug=True)
 
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
3
 
 
4
  spotify_embed = """
5
  <iframe data-testid="embed-iframe"
6
  style="border-radius:12px"
 
14
  encrypted-media;
15
  fullscreen;
16
  picture-in-picture"
17
+ loading="lazy"></iframe>"""
 
18
 
 
19
  theme = gr.themes.Soft().set(
20
 
21
  body_background_fill="#fff7fb",
 
42
  link_text_color="#c77dff"
43
  )
44
 
45
+ with open("knowledge.txt" , "r", encoding="utf-8") as f:
 
46
  knowledge_base = f.read()
47
 
 
48
  client = InferenceClient("Qwen/Qwen2.5-7B-Instruct")
49
 
 
50
  SYSTEM_MESSAGES = {
 
51
  "wellness": (
52
  "You are a kind wellness chatbot. "
53
  "Give practical, supportive, and thoughtful advice "
 
62
  }
63
 
64
 
 
65
  def respond(message, history, mode):
66
 
 
67
  if mode is None:
 
 
 
 
 
68
 
69
+ user_choice = message.lower().strip()
70
+
71
+ if user_choice in ["wellness", "wellness mode"]:
72
+ mode = "wellness"
73
+ yield (
74
+ "🌿 Wellness mode activated!\n\n"
75
+ "Tell me what's on your mind.",
76
+ mode
77
+ )
78
+ return
79
+
80
+ if user_choice in ["story", "storytelling", "creative"]: #changed elif to if to let the user change choices anytime
81
+ mode = "story"
82
+ yield (
83
+ "📖 Creative Storytelling mode activated!\n\n"
84
+ "Give me a story idea!",
85
+ mode
86
+ )
87
+ return
88
+
89
+ else:
90
+ yield (
91
+ "Hi! How can I help you today?\n\n"
92
+ "• wellness\n"
93
+ "• story",
94
+ mode
95
+ )
96
+ return
97
+
98
  messages = [
99
  {
100
  "role": "system",
 
102
  }
103
  ]
104
 
 
105
  if history:
106
  messages.extend(history)
107
 
 
108
  messages.append({
109
+ "role": "user",
110
+ "content": message
111
  })
112
+
113
  response = ""
114
 
 
115
  for chunk in client.chat_completion(
116
  messages=messages,
117
  max_tokens=1024,
 
120
  stream=True,
121
  ):
122
 
123
+ token = chunk.choices[0].delta.content or ""
124
 
125
  if token:
126
  response += token
127
  yield response, mode
128
 
129
 
 
130
  with gr.Blocks(theme=theme) as demo:
131
 
 
132
  mode_state = gr.State(None)
133
 
134
+ gr.Image("Website Banner.png", show_label=False, container=False)
 
 
 
 
 
135
 
 
136
  gr.Markdown("# ***Wellness and Storytelling ChatBot***")
137
 
138
  gr.Markdown(
139
+ "DISCLAIMER: While signing, please spell out individual letters in ASL instead of the distinct signs \n\n"
140
+ "Choose a mode by typing:\n\n"
141
+ "- `wellness`\n"
142
+ "- `story`"
143
  )
144
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
145
  chatbot = gr.ChatInterface(
146
  fn=respond,
147
  additional_inputs=[mode_state],
148
+ additional_outputs=[mode_state],
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
149
  )
150
 
 
151
  gr.Markdown("### 🎵 Music We Listened To While Building This")
152
 
153
  gr.HTML(spotify_embed)
154
 
 
155
  demo.launch(debug=True)