Mariam444 commited on
Commit
867af06
Β·
verified Β·
1 Parent(s): 5d8a45d

pleaseyrab

Browse files
Files changed (1) hide show
  1. app.py +151 -67
app.py CHANGED
@@ -1,103 +1,187 @@
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
3
  import os
4
- import traceback
5
 
6
- # Get token
7
  HF_TOKEN = os.getenv("HF_TOKEN") or os.getenv("HUGGING_FACE_HUB_TOKEN")
8
 
9
- print("\n" + "="*60)
10
- print("πŸ” DIAGNOSTIC MODE - ENHANCED ERROR REPORTING")
11
- print("="*60)
12
- print(f"Token exists: {bool(HF_TOKEN)}")
13
- if HF_TOKEN:
14
- print(f"Token preview: {HF_TOKEN[:20]}...{HF_TOKEN[-10:]}")
15
- print("="*60 + "\n")
16
 
17
- def respond(message, history):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
  if not HF_TOKEN:
19
- return "❌ ERROR: No HF_TOKEN found in secrets."
 
20
 
21
- models = [
22
- "mistralai/Mistral-7B-Instruct-v0.1",
23
- "google/gemma-2b-it",
24
- "microsoft/phi-2",
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25
  ]
26
 
27
- errors_log = []
28
 
29
- for model_name in models:
 
30
  try:
31
- print(f"\n{'='*60}")
32
- print(f"πŸ”„ Attempting: {model_name}")
33
- print(f"{'='*60}")
34
-
35
- # Create client
36
- client = InferenceClient(token=HF_TOKEN)
37
-
38
- # Try chat completion
39
- messages = [
40
- {"role": "system", "content": "You are a helpful assistant."},
41
- {"role": "user", "content": message}
42
- ]
43
-
44
- print(f"Calling chat_completion with model={model_name}")
45
 
46
- response = ""
47
  for msg in client.chat_completion(
48
  messages,
49
  model=model_name,
50
- max_tokens=100,
51
  stream=True,
 
 
52
  ):
53
  token = msg.choices[0].delta.content or ""
54
  response += token
 
55
 
56
  print(f"βœ… SUCCESS with {model_name}")
57
- print(f"Response: {response[:100]}")
58
- return response
59
 
60
  except Exception as e:
61
- # Capture FULL error details
62
- error_type = type(e).__name__
63
- error_msg = str(e)
64
- error_trace = traceback.format_exc()
65
-
66
- print(f"❌ FAILED with {model_name}")
67
- print(f"Error Type: {error_type}")
68
- print(f"Error Message: {error_msg}")
69
- print(f"Full Traceback:\n{error_trace}")
70
- print("="*60)
71
 
72
- errors_log.append({
73
- "model": model_name,
74
- "type": error_type,
75
- "message": error_msg,
76
- "trace": error_trace
77
- })
 
 
 
 
 
 
78
 
79
  continue
80
 
81
- # All models failed - return detailed error report
82
- error_report = "❌ ALL MODELS FAILED\n\n"
 
 
83
 
84
- for idx, err in enumerate(errors_log, 1):
85
- error_report += f"Model {idx}: {err['model']}\n"
86
- error_report += f"Error: {err['type']}\n"
87
- error_report += f"Message: {err['message'][:200]}\n"
88
- error_report += "-"*40 + "\n\n"
 
 
 
89
 
90
- error_report += "\nπŸ” Check the Logs tab for full stack traces."
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
91
 
92
- return error_report
 
 
 
 
 
 
 
 
93
 
94
- # Simple interface
95
- demo = gr.Interface(
96
- fn=respond,
97
- inputs=gr.Textbox(label="Test message", value="Hello, how are you?"),
98
- outputs=gr.Textbox(label="Response", lines=15),
99
- title="πŸ” Enhanced Diagnostic Mode",
100
- description="Testing all models with detailed error logging. Check Logs tab for full details.",
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
101
  )
102
 
103
  if __name__ == "__main__":
 
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
3
  import os
4
+ import re
5
 
6
+ # Load token
7
  HF_TOKEN = os.getenv("HF_TOKEN") or os.getenv("HUGGING_FACE_HUB_TOKEN")
8
 
9
+ print("="*50)
10
+ if not HF_TOKEN:
11
+ print("❌ HF_TOKEN NOT FOUND")
12
+ else:
13
+ print(f"βœ… HF_TOKEN Found: {HF_TOKEN[:15]}...{HF_TOKEN[-8:]}")
14
+ print("="*50)
 
15
 
16
+ OFF_TOPIC_REGEX = r"(http|www|buy now|discount|subscribe|follow me|click here)"
17
+
18
+ def is_safe_to_process(text: str) -> bool:
19
+ if len(text.strip()) < 2:
20
+ return False
21
+ if re.search(OFF_TOPIC_REGEX, text.lower()):
22
+ return False
23
+ return True
24
+
25
+ def respond(
26
+ message,
27
+ history: list[dict[str, str]],
28
+ system_message,
29
+ max_tokens,
30
+ temperature,
31
+ top_p,
32
+ request: gr.Request,
33
+ ):
34
+ user_name = "User"
35
+ if request:
36
+ user_name = request.query_params.get("username", "User")
37
+
38
+ if not is_safe_to_process(message):
39
+ yield f"Hello {user_name}, I'm here to support your emotional well-being. How can I help you today?"
40
+ return
41
+
42
  if not HF_TOKEN:
43
+ yield "❌ Configuration Error: HF_TOKEN not found in Space secrets. Please add it in Settings."
44
+ return
45
 
46
+ personalized_system = (
47
+ f"{system_message} "
48
+ f"The user's name is {user_name}. Address them naturally by name. "
49
+ f"Be conversational, not formal."
50
+ )
51
+
52
+ messages = [{"role": "system", "content": personalized_system}]
53
+ messages.extend(history[-10:])
54
+ messages.append({"role": "user", "content": message})
55
+
56
+ # Create client once
57
+ client = InferenceClient(token=HF_TOKEN)
58
+
59
+ # Try chat_completion with these models
60
+ chat_models = [
61
+ "meta-llama/Meta-Llama-3-8B-Instruct",
62
+ "mistralai/Mistral-7B-Instruct-v0.2",
63
+ "HuggingFaceH4/zephyr-7b-beta",
64
  ]
65
 
66
+ response = ""
67
 
68
+ # Try chat_completion first
69
+ for model_name in chat_models:
70
  try:
71
+ print(f"\n{'='*50}")
72
+ print(f"πŸ”„ Trying chat_completion: {model_name}")
73
+ print(f"{'='*50}")
 
 
 
 
 
 
 
 
 
 
 
74
 
 
75
  for msg in client.chat_completion(
76
  messages,
77
  model=model_name,
78
+ max_tokens=max_tokens,
79
  stream=True,
80
+ temperature=temperature,
81
+ top_p=top_p,
82
  ):
83
  token = msg.choices[0].delta.content or ""
84
  response += token
85
+ yield response
86
 
87
  print(f"βœ… SUCCESS with {model_name}")
88
+ return
 
89
 
90
  except Exception as e:
91
+ error_str = str(e)
92
+ error_repr = repr(e)
93
+ print(f"❌ Failed with {model_name}")
94
+ print(f"Error str: {error_str}")
95
+ print(f"Error repr: {error_repr}")
96
+ print(f"Error type: {type(e).__name__}")
 
 
 
 
97
 
98
+ # Check for auth errors
99
+ if any(x in error_str for x in ["401", "403", "Unauthorized", "Forbidden"]):
100
+ print("🚨 AUTHENTICATION ERROR")
101
+ yield (
102
+ f"πŸ” Authentication Problem!\n\n"
103
+ f"Your token isn't working. Please:\n"
104
+ f"1. Create a NEW token at https://huggingface.co/settings/tokens\n"
105
+ f"2. Add it in Space Settings β†’ Secrets as 'HF_TOKEN'\n"
106
+ f"3. Restart this Space\n\n"
107
+ f"Error: {error_str}"
108
+ )
109
+ return
110
 
111
  continue
112
 
113
+ # If chat_completion failed, try text_generation as fallback
114
+ print("\n" + "="*50)
115
+ print("⚠️ All chat models failed, trying text_generation fallback")
116
+ print("="*50)
117
 
118
+ text_models = [
119
+ "microsoft/phi-2",
120
+ "google/flan-t5-large",
121
+ "bigscience/bloom-560m",
122
+ ]
123
+
124
+ # Build a simple prompt from the conversation
125
+ prompt = f"{personalized_system}\n\nUser: {message}\nAssistant:"
126
 
127
+ for model_name in text_models:
128
+ try:
129
+ print(f"πŸ”„ Trying text_generation: {model_name}")
130
+
131
+ result = client.text_generation(
132
+ prompt,
133
+ model=model_name,
134
+ max_new_tokens=max_tokens,
135
+ temperature=temperature,
136
+ top_p=top_p,
137
+ stream=True,
138
+ )
139
+
140
+ response = ""
141
+ for token in result:
142
+ response += token
143
+ yield response
144
+
145
+ print(f"βœ… SUCCESS with text_generation: {model_name}")
146
+ return
147
+
148
+ except Exception as e:
149
+ print(f"❌ Failed text_generation with {model_name}: {repr(e)}")
150
+ continue
151
 
152
+ # Everything failed
153
+ yield (
154
+ f"I'm here for you, {user_name}, but I'm experiencing connection issues with the AI service. "
155
+ f"This could be due to:\n\n"
156
+ f"β€’ High demand on Hugging Face servers\n"
157
+ f"β€’ Models are loading (cold start)\n"
158
+ f"β€’ Network connectivity issues\n\n"
159
+ f"Please try again in 30-60 seconds. If this persists, check the Space logs for details."
160
+ )
161
 
162
+ # Gradio Interface
163
+ demo = gr.ChatInterface(
164
+ respond,
165
+ type="messages",
166
+ title="πŸ’™ Mariam - Mental Health Support",
167
+ description="A compassionate AI assistant for emotional well-being",
168
+ additional_inputs=[
169
+ gr.Textbox(
170
+ value=(
171
+ "You are Mariam, a compassionate mental health assistant. "
172
+ "Respond directly and naturally to the user. "
173
+ "Be warm, empathetic, and professional. "
174
+ "Never use formal letter formats."
175
+ ),
176
+ label="System Message",
177
+ lines=4,
178
+ ),
179
+ gr.Slider(128, 1024, value=512, label="Max Tokens", step=32),
180
+ gr.Slider(0.1, 1.5, value=0.7, step=0.1, label="Temperature"),
181
+ gr.Slider(0.1, 1.0, value=0.9, step=0.05, label="Top-p"),
182
+ ],
183
+ theme=gr.themes.Soft(primary_hue="blue"),
184
+ cache_examples=False,
185
  )
186
 
187
  if __name__ == "__main__":