Spaces:
Sleeping
Sleeping
pleaseyrab
Browse files
app.py
CHANGED
|
@@ -1,103 +1,187 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
import os
|
| 4 |
-
import
|
| 5 |
|
| 6 |
-
#
|
| 7 |
HF_TOKEN = os.getenv("HF_TOKEN") or os.getenv("HUGGING_FACE_HUB_TOKEN")
|
| 8 |
|
| 9 |
-
print("
|
| 10 |
-
|
| 11 |
-
print("
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
print("="*60 + "\n")
|
| 16 |
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
if not HF_TOKEN:
|
| 19 |
-
|
|
|
|
| 20 |
|
| 21 |
-
|
| 22 |
-
"
|
| 23 |
-
"
|
| 24 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
]
|
| 26 |
|
| 27 |
-
|
| 28 |
|
| 29 |
-
|
|
|
|
| 30 |
try:
|
| 31 |
-
print(f"\n{'='*
|
| 32 |
-
print(f"π
|
| 33 |
-
print(f"{'='*
|
| 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=
|
| 51 |
stream=True,
|
|
|
|
|
|
|
| 52 |
):
|
| 53 |
token = msg.choices[0].delta.content or ""
|
| 54 |
response += token
|
|
|
|
| 55 |
|
| 56 |
print(f"β
SUCCESS with {model_name}")
|
| 57 |
-
|
| 58 |
-
return response
|
| 59 |
|
| 60 |
except Exception as e:
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
print(f"
|
| 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 |
-
|
| 73 |
-
|
| 74 |
-
"
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
|
| 79 |
continue
|
| 80 |
|
| 81 |
-
#
|
| 82 |
-
|
|
|
|
|
|
|
| 83 |
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
|
|
|
|
|
|
|
|
|
| 89 |
|
| 90 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
|
| 92 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
|
| 94 |
-
#
|
| 95 |
-
demo = gr.
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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__":
|