razaali10 commited on
Commit
e5d5895
·
verified ·
1 Parent(s): 8a65ae1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -159
app.py CHANGED
@@ -1,185 +1,98 @@
1
  import os
2
  import re
3
- import json
4
- import requests
5
  import gradio as gr
 
6
 
7
- # ----------------------------------------------------------
8
- # Hugging Face Inference API configuration
9
- # ----------------------------------------------------------
10
-
11
  MODEL_ID = "Psychotherapy-LLM/PsychoCounsel-Llama3-8B"
12
 
13
- # Use the new Router endpoint (old api-inference.huggingface.co is deprecated)
14
- API_URL = f"https://router.huggingface.co/hf-inference/models/{MODEL_ID}"
15
-
16
- # Set this in your Space secrets: HF_TOKEN
17
  HF_TOKEN = os.getenv("HF_TOKEN")
18
 
 
 
 
 
19
 
20
- def call_inference_api(prompt: str, max_new_tokens: int, temperature: float, top_p: float) -> str:
21
- """
22
- Call the Hugging Face Inference API via raw HTTP.
23
-
24
- This avoids streaming / generators (which were causing StopIteration)
25
- and works fine on CPU Spaces.
26
- """
27
- if not HF_TOKEN:
28
- return (
29
- "⚠️ HF_TOKEN environment variable is not set.\n\n"
30
- "Go to your Space → Settings → Repository secrets and add HF_TOKEN."
31
- )
32
-
33
- headers = {
34
- "Authorization": f"Bearer {HF_TOKEN}",
35
- "Content-Type": "application/json",
36
- }
37
-
38
- payload = {
39
- "inputs": prompt,
40
- "parameters": {
41
- "max_new_tokens": int(max_new_tokens),
42
- "temperature": float(temperature),
43
- "top_p": float(top_p),
44
- "do_sample": True,
45
- },
46
- "options": {
47
- "wait_for_model": True, # wait for cold start
48
- },
49
- }
50
-
51
- try:
52
- response = requests.post(API_URL, headers=headers, data=json.dumps(payload), timeout=240)
53
- except Exception as e:
54
- return f"⚠️ Network / request error:\n{type(e).__name__}: {e}"
55
-
56
- if response.status_code != 200:
57
- try:
58
- err = response.json()
59
- except Exception:
60
- err = response.text
61
- return f"⚠️ HF Inference API returned status {response.status_code}:\n{err}"
62
-
63
- try:
64
- data = response.json()
65
- except Exception as e:
66
- return f"⚠️ Could not parse JSON from HF Inference API:\n{type(e).__name__}: {e}"
67
-
68
- # Text-generation responses are usually a list of dicts with "generated_text"
69
- text = None
70
- if isinstance(data, list) and data and isinstance(data[0], dict) and "generated_text" in data[0]:
71
- text = data[0]["generated_text"]
72
- elif isinstance(data, dict) and "generated_text" in data:
73
- text = data["generated_text"]
74
-
75
- if text is None:
76
- # Fallback: show raw response for debugging
77
- text = str(data)
78
-
79
- return text
80
-
81
-
82
- # ----------------------------------------------------------
83
- # Prompt builder
84
- # ----------------------------------------------------------
85
 
 
 
 
86
  def build_prompt(client_speech: str, context: str, mode: str) -> str:
87
- client_speech = (client_speech or "").strip()
88
- context = (context or "").strip()
89
-
90
  if mode == "Brief (5–7 sentences)":
91
  instruction = (
92
- "You are a professional psychotherapist conducting a session with a client. "
93
- "Write 5–7 sentences in a warm, empathic, reflective tone, similar to the "
94
- "PsychoCounsel-Llama3-8B Appendix case studies. Ask a few gentle open-ended "
95
- "questions. Only output what the therapist says to the client."
96
  )
97
  else:
98
  instruction = (
99
- "You are a professional psychotherapist conducting a session with a client. "
100
- "Generate a detailed multi-paragraph therapeutic response in the tone and "
101
- "structure of the Appendix case study for PsychoCounsel-Llama3-8B. Start with "
102
- "validation and normalization, explore fears and beliefs, reflect on self-trust "
103
- "and values, consider introducing a simple exercise, and close by inviting the "
104
- "client to share what resonates. Only output what the therapist says."
105
  )
106
 
107
  if context:
108
  instruction += f" Therapist context: {context}"
109
 
110
- prompt = f"""{instruction}
111
 
112
  Client Speech:
113
  {client_speech}
114
 
115
  Therapist:
116
  """
117
- return prompt
118
-
119
 
120
- # ----------------------------------------------------------
121
- # Main generation function
122
- # ----------------------------------------------------------
123
 
124
- def generate_response(
125
- client_speech: str,
126
- context: str,
127
- mode: str,
128
- temperature: float,
129
- top_p: float,
130
- ):
131
  if not client_speech or not client_speech.strip():
132
  return "⚠️ Please enter client speech."
133
 
134
  prompt = build_prompt(client_speech, context, mode)
135
 
136
- if mode == "Brief (5–7 sentences)":
137
- max_tokens = 220
138
- else:
139
- max_tokens = 450
140
-
141
- raw_text = call_inference_api(prompt, max_tokens, temperature, top_p)
142
 
143
- # If call_inference_api returned an error message, show it directly
144
- if raw_text.startswith("⚠️"):
145
- return raw_text
 
 
 
 
 
 
 
 
146
 
147
- # Clean model artefacts if present
148
- text = raw_text.strip()
149
  text = text.split("Note:")[0].split("FINAL ANSWER")[0].strip()
150
 
 
151
  if mode == "Brief (5–7 sentences)":
152
  sents = re.split(r"(?<=[.!?])\s+", text)
153
- sents = [s.strip() for s in sents if s.strip()]
154
  text = " ".join(sents[:7])
155
 
156
  return text
157
 
158
 
159
- # ----------------------------------------------------------
160
- # Gradio UI
161
- # ----------------------------------------------------------
 
 
 
 
162
 
163
  DESCRIPTION = (
164
- "This demo reproduces Appendix-style therapist responses from the paper using "
165
- f"**{MODEL_ID}**.\n\n"
166
- "> ⚠️ **Not for clinical use.** This is a research / replication demo only and is "
167
- "not a substitute for real-world psychiatric or psychological care."
168
  )
169
 
170
- DEFAULT_CLIENT_SPEECH = (
171
- "I’ve been having emotional issues for a few years. Nonetheless, these have been somewhat "
172
- "manageable. However, I became increasingly paranoid this winter. I thought that people were "
173
- "trying to poison me, I feared that family members were going to kill me. I was aware that "
174
- "these thoughts were illogical, but I couldn’t shake the fear that they caused me. I would get "
175
- "panic attacks thinking that I drank from a poisoned water fountain. I thought that someone had "
176
- "put small rips in my ice cream sandwich packaging because there was only one that didn’t have "
177
- "tiny rips in it. I refused to eat this one because I assumed some reverse psychology was being "
178
- "pulled and the ones without the rips were fine. I thought that this was mostly caused by anxiety "
179
- "but there are other symptoms that lead me to believe it is something more."
180
- )
181
 
182
- with gr.Blocks(title="PsychoCounsel-Llama3-8B — Original / Research Demo") as demo:
183
  gr.Markdown("# 🧠 PsychoCounsel-Llama3-8B — Original / Research Demo")
184
  gr.Markdown(DESCRIPTION)
185
 
@@ -188,46 +101,30 @@ with gr.Blocks(title="PsychoCounsel-Llama3-8B — Original / Research Demo") as
188
  mode_radio = gr.Radio(
189
  ["Brief (5–7 sentences)", "Extended (Appendix-style)"],
190
  value="Extended (Appendix-style)",
191
- label="Response Style",
192
- )
193
- temperature_slider = gr.Slider(
194
- minimum=0.1,
195
- maximum=1.0,
196
- value=0.6,
197
- step=0.05,
198
- label="Temperature",
199
- )
200
- top_p_slider = gr.Slider(
201
- minimum=0.5,
202
- maximum=1.0,
203
- value=0.9,
204
- step=0.05,
205
- label="Top-p",
206
- )
207
- gr.Markdown(
208
- "This version is for **research / replication** and may generate content "
209
- "that is not appropriate for direct use with vulnerable clients."
210
  )
 
 
211
 
212
  with gr.Column(scale=2):
213
- client_speech_box = gr.Textbox(
214
  label="Client Speech",
215
  value=DEFAULT_CLIENT_SPEECH,
216
- lines=12,
217
  )
218
  context_box = gr.Textbox(
219
- label="Optional Therapist Context (modality, goals, etc.)",
220
  value="",
221
- lines=4,
222
  )
223
 
224
  generate_btn = gr.Button("Generate Therapist Response", variant="primary")
225
- output_box = gr.Markdown(label="Therapist Response (Model Output)")
226
 
227
  generate_btn.click(
228
- fn=generate_response,
229
- inputs=[client_speech_box, context_box, mode_radio, temperature_slider, top_p_slider],
230
- outputs=output_box,
231
  )
232
 
233
  if __name__ == "__main__":
 
1
  import os
2
  import re
 
 
3
  import gradio as gr
4
+ from huggingface_hub import InferenceClient
5
 
6
+ # ----------------------------------------
7
+ # HF Setup (use HF Router)
8
+ # ----------------------------------------
 
9
  MODEL_ID = "Psychotherapy-LLM/PsychoCounsel-Llama3-8B"
10
 
 
 
 
 
11
  HF_TOKEN = os.getenv("HF_TOKEN")
12
 
13
+ if HF_TOKEN:
14
+ client = InferenceClient(model=MODEL_ID, token=HF_TOKEN)
15
+ else:
16
+ client = InferenceClient(model=MODEL_ID)
17
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
 
19
+ # ----------------------------------------
20
+ # Build prompt
21
+ # ----------------------------------------
22
  def build_prompt(client_speech: str, context: str, mode: str) -> str:
 
 
 
23
  if mode == "Brief (5–7 sentences)":
24
  instruction = (
25
+ "You are a professional psychotherapist. Respond in 5–7 warm, reflective, "
26
+ "empathic sentences. Only output what the therapist says."
 
 
27
  )
28
  else:
29
  instruction = (
30
+ "You are a professional psychotherapist. Produce a detailed multi-paragraph "
31
+ "therapeutic response similar to the Appendix case studies. Start with validation, "
32
+ "explore beliefs, discuss values, suggest a small exercise, and end with an open question."
 
 
 
33
  )
34
 
35
  if context:
36
  instruction += f" Therapist context: {context}"
37
 
38
+ return f"""{instruction}
39
 
40
  Client Speech:
41
  {client_speech}
42
 
43
  Therapist:
44
  """
 
 
45
 
 
 
 
46
 
47
+ # ----------------------------------------
48
+ # Generate
49
+ # ----------------------------------------
50
+ def generate_response(client_speech, context, mode, temperature, top_p):
 
 
 
51
  if not client_speech or not client_speech.strip():
52
  return "⚠️ Please enter client speech."
53
 
54
  prompt = build_prompt(client_speech, context, mode)
55
 
56
+ max_tokens = 220 if mode == "Brief (5–7 sentences)" else 450
 
 
 
 
 
57
 
58
+ try:
59
+ output = client.text_generation(
60
+ prompt,
61
+ max_new_tokens=max_tokens,
62
+ temperature=float(temperature),
63
+ top_p=float(top_p),
64
+ do_sample=True,
65
+ return_full_text=False,
66
+ )
67
+ except Exception as e:
68
+ return f"⚠️ HF API Error: {e}"
69
 
70
+ text = output.strip()
 
71
  text = text.split("Note:")[0].split("FINAL ANSWER")[0].strip()
72
 
73
+ # Limit to 7 sentences in brief mode
74
  if mode == "Brief (5–7 sentences)":
75
  sents = re.split(r"(?<=[.!?])\s+", text)
 
76
  text = " ".join(sents[:7])
77
 
78
  return text
79
 
80
 
81
+ # ----------------------------------------
82
+ # UI
83
+ # ----------------------------------------
84
+ DEFAULT_CLIENT_SPEECH = (
85
+ "I’ve been having emotional issues for a few years. Nonetheless, these have been somewhat "
86
+ "manageable. However, I became increasingly paranoid this winter..."
87
+ )
88
 
89
  DESCRIPTION = (
90
+ "This replicates Appendix-style psychotherapist responses using PsychoCounsel-Llama3-8B.\n\n"
91
+ "⚠️ Not for clinical use. Research demo only."
 
 
92
  )
93
 
94
+ with gr.Blocks(title="PsychoCounsel-Llama3-8B — Research Demo") as demo:
 
 
 
 
 
 
 
 
 
 
95
 
 
96
  gr.Markdown("# 🧠 PsychoCounsel-Llama3-8B — Original / Research Demo")
97
  gr.Markdown(DESCRIPTION)
98
 
 
101
  mode_radio = gr.Radio(
102
  ["Brief (5–7 sentences)", "Extended (Appendix-style)"],
103
  value="Extended (Appendix-style)",
104
+ label="Response Style"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
105
  )
106
+ temp = gr.Slider(0.1, 1.0, value=0.6, step=0.05, label="Temperature")
107
+ top_p = gr.Slider(0.5, 1.0, value=0.9, step=0.05, label="Top-p")
108
 
109
  with gr.Column(scale=2):
110
+ speech_box = gr.Textbox(
111
  label="Client Speech",
112
  value=DEFAULT_CLIENT_SPEECH,
113
+ lines=12
114
  )
115
  context_box = gr.Textbox(
116
+ label="Optional Therapist Context",
117
  value="",
118
+ lines=3
119
  )
120
 
121
  generate_btn = gr.Button("Generate Therapist Response", variant="primary")
122
+ output = gr.Markdown()
123
 
124
  generate_btn.click(
125
+ generate_response,
126
+ inputs=[speech_box, context_box, mode_radio, temp, top_p],
127
+ outputs=output,
128
  )
129
 
130
  if __name__ == "__main__":