koush1ki commited on
Commit
3fab412
·
verified ·
1 Parent(s): fcd300a

copied the main to work on it here

Browse files
Files changed (1) hide show
  1. app.py +106 -0
app.py ADDED
@@ -0,0 +1,106 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from huggingface_hub import InferenceClient
3
+
4
+ with open("knowledge.txt" , "r", encoding="utf-8") as f:
5
+ knowledge_base = f.read()
6
+
7
+ client = InferenceClient("Qwen/Qwen2.5-7B-Instruct")
8
+
9
+ SYSTEM_MESSAGES = {
10
+ "wellness": (
11
+ "You are a kind wellness chatbot. "
12
+ "Give practical, supportive, and thoughtful advice "
13
+ "about the issues the user shares."
14
+ ),
15
+
16
+ "story": (
17
+ "You are a creative storytelling assistant. "
18
+ "Create imaginative, engaging, and detailed stories "
19
+ "based on the user's ideas."
20
+ )
21
+ }
22
+
23
+
24
+ def respond(message, history, mode):
25
+
26
+ if mode is None:
27
+
28
+ user_choice = message.lower().strip()
29
+
30
+ if user_choice in ["wellness", "wellness mode"]:
31
+ mode = "wellness"
32
+ yield (
33
+ "🌿 Wellness mode activated!\n\n"
34
+ "Tell me what's on your mind.",
35
+ mode
36
+ )
37
+ return
38
+
39
+ if user_choice in ["story", "storytelling", "creative"]: #changed elif to if to let the user change choices anytime
40
+ mode = "story"
41
+ yield (
42
+ "📖 Creative Storytelling mode activated!\n\n"
43
+ "Give me a story idea!",
44
+ mode
45
+ )
46
+ return
47
+
48
+ else:
49
+ yield (
50
+ "Please choose a mode first:\n\n"
51
+ "• wellness\n"
52
+ "• story",
53
+ mode
54
+ )
55
+ return
56
+
57
+ messages = [
58
+ {
59
+ "role": "system",
60
+ "content": SYSTEM_MESSAGES[mode] + "\n\n" + knowledge_base
61
+ }
62
+ ]
63
+
64
+ if history:
65
+ messages.extend(history)
66
+
67
+ messages.append({
68
+ "role": "user",
69
+ "content": message
70
+ })
71
+
72
+ response = ""
73
+
74
+ for chunk in client.chat_completion(
75
+ messages=messages,
76
+ max_tokens=256,
77
+ temperature=0.7,
78
+ top_p=0.9,
79
+ stream=True,
80
+ ):
81
+
82
+ token = chunk.choices[0].delta.content or "" #added quotations to prevent interfaces from returning chunks when none
83
+
84
+ if token:
85
+ response += token
86
+ yield response, mode
87
+
88
+
89
+ with gr.Blocks() as demo:
90
+
91
+ mode_state = gr.State(None)
92
+
93
+ gr.Markdown("# Wellness and Storytelling Chatbot")
94
+ gr.Markdown(
95
+ "Choose a mode by typing:\n\n"
96
+ "- `wellness`\n"
97
+ "- `story`"
98
+ )
99
+
100
+ chatbot = gr.ChatInterface(
101
+ fn=respond,
102
+ additional_inputs=[mode_state],
103
+ additional_outputs=[mode_state],
104
+ )
105
+
106
+ demo.launch(debug=True)