DetectiveShadow commited on
Commit
c746dee
Β·
verified Β·
1 Parent(s): 9ac3ac2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -24
app.py CHANGED
@@ -4,6 +4,7 @@ import random
4
 
5
  client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
6
 
 
7
  quotes = [
8
  "When anger rises, think of the consequences.",
9
  "You cannot control the wind, but you can adjust your sails.",
@@ -11,51 +12,61 @@ quotes = [
11
  "Peace begins with a single thought.",
12
  "Don't let a moment of anger become a lifetime of regret."
13
  ]
14
- quote_of_the_day = random.choice(quotes)
15
-
16
- def respond(message, history):
17
- system_message = "You are a calming and supportive emotional wellness chatbot named Aggro."
18
 
19
- if not history:
20
- return {
21
- "role": "assistant",
22
- "content": f"Hi, I'm Aggro! 🌱\nQuote of the Day: \"{quote_of_the_day}\"\nHow is your day going?"
23
- }
24
 
 
25
  lowered = message.lower()
 
26
  if "angry" in lowered:
27
- return {"role": "assistant", "content": "I'm sorry you're feeling angry. Let's take a deep breath. Want to try a calming exercise?"}
 
28
  elif "sad" in lowered:
29
- return {"role": "assistant", "content": "It's okay to feel sad. I'm here to support you. πŸ’™"}
 
30
  elif "stressed" in lowered:
31
- return {"role": "assistant", "content": "Let’s pause and breathe. I’m here with you. 🧘"}
 
32
  elif "calm" in lowered:
33
- return {"role": "assistant", "content": "That's great to hear! 🌿 Keep holding on to that calm energy."}
 
34
 
35
  messages = [{"role": "system", "content": system_message}]
36
- for m in history:
37
- messages.append(m)
 
 
 
 
38
  messages.append({"role": "user", "content": message})
39
 
40
  response = ""
41
  for msg in client.chat_completion(
42
  messages,
43
- max_tokens=512,
44
  stream=True,
45
- temperature=0.7,
46
- top_p=0.95,
47
  ):
48
  token = msg.choices[0].delta.content
49
  response += token
 
50
 
51
- return {"role": "assistant", "content": response}
52
-
53
- # βœ… Build the interface
54
  demo = gr.ChatInterface(
55
- fn=respond,
56
- chatbot=gr.Chatbot(label="Aggro πŸ’¬", type="messages"),
 
 
 
 
 
 
 
 
 
57
  title="πŸ’š Meet Aggro - Your Emotion Companion",
58
- description=f"A calming chatbot to help you reflect and feel supported.\n\nπŸ’¬ _Quote of the Day:_ \"{quote_of_the_day}\""
59
  )
60
 
61
  if __name__ == "__main__":
 
4
 
5
  client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
6
 
7
+ # List of motivational quotes
8
  quotes = [
9
  "When anger rises, think of the consequences.",
10
  "You cannot control the wind, but you can adjust your sails.",
 
12
  "Peace begins with a single thought.",
13
  "Don't let a moment of anger become a lifetime of regret."
14
  ]
 
 
 
 
15
 
16
+ quote_of_the_day = random.choice(quotes)
 
 
 
 
17
 
18
+ def respond(message, history, system_message, max_tokens, temperature, top_p):
19
  lowered = message.lower()
20
+
21
  if "angry" in lowered:
22
+ yield "I'm sorry you're feeling angry. Let's take a deep breath together. Would you like to try a calming exercise?"
23
+ return
24
  elif "sad" in lowered:
25
+ yield "It's okay to feel sad sometimes. I'm here to support you. πŸ’™"
26
+ return
27
  elif "stressed" in lowered:
28
+ yield "Let's pause and take a deep breath. I'm here for you."
29
+ return
30
  elif "calm" in lowered:
31
+ yield "That's great to hear! Stay present and peaceful. 🌿"
32
+ return
33
 
34
  messages = [{"role": "system", "content": system_message}]
35
+ for user_msg, bot_msg in history:
36
+ if user_msg:
37
+ messages.append({"role": "user", "content": user_msg})
38
+ if bot_msg:
39
+ messages.append({"role": "assistant", "content": bot_msg})
40
+
41
  messages.append({"role": "user", "content": message})
42
 
43
  response = ""
44
  for msg in client.chat_completion(
45
  messages,
46
+ max_tokens=max_tokens,
47
  stream=True,
48
+ temperature=temperature,
49
+ top_p=top_p,
50
  ):
51
  token = msg.choices[0].delta.content
52
  response += token
53
+ yield response
54
 
55
+ # UI layout
 
 
56
  demo = gr.ChatInterface(
57
+ respond,
58
+ additional_inputs=[
59
+ gr.Textbox(
60
+ value="You are a calming and supportive emotional wellness assistant named Aggro.",
61
+ label="System message",
62
+ visible=False
63
+ ),
64
+ gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
65
+ gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
66
+ gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)"),
67
+ ],
68
  title="πŸ’š Meet Aggro - Your Emotion Companion",
69
+ description=f"πŸ’¬ Quote of the Day: _{quote_of_the_day}_"
70
  )
71
 
72
  if __name__ == "__main__":