Update app.py
Browse files
app.py
CHANGED
|
@@ -169,10 +169,10 @@ HF_TOKEN = os.getenv("HF_TOKEN")
|
|
| 169 |
if not HF_TOKEN:
|
| 170 |
raise ValueError("HF_TOKEN is missing.")
|
| 171 |
|
| 172 |
-
# Initialize client with
|
| 173 |
client = InferenceClient(
|
|
|
|
| 174 |
api_key=HF_TOKEN,
|
| 175 |
-
provider="nscale", # <-- explicitly set the provider here
|
| 176 |
)
|
| 177 |
|
| 178 |
# System prompt
|
|
@@ -185,29 +185,47 @@ system_message = (
|
|
| 185 |
|
| 186 |
def chat_function(message, history):
|
| 187 |
|
| 188 |
-
messages = [
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 189 |
|
| 190 |
history = history or []
|
| 191 |
|
| 192 |
for item in history:
|
|
|
|
| 193 |
if isinstance(item, dict):
|
| 194 |
role = item.get("role")
|
| 195 |
content = item.get("content", "")
|
| 196 |
if role in ["user", "assistant"]:
|
| 197 |
-
messages.append({
|
|
|
|
|
|
|
|
|
|
| 198 |
|
| 199 |
elif isinstance(item, (list, tuple)) and len(item) == 2:
|
| 200 |
user_msg, assistant_msg = item
|
| 201 |
if user_msg:
|
| 202 |
-
messages.append({
|
|
|
|
|
|
|
|
|
|
| 203 |
if assistant_msg:
|
| 204 |
-
messages.append({
|
|
|
|
|
|
|
|
|
|
| 205 |
|
| 206 |
-
messages.append({
|
|
|
|
|
|
|
|
|
|
| 207 |
|
| 208 |
try:
|
| 209 |
completion = client.chat.completions.create(
|
| 210 |
-
model="Qwen/Qwen2.5-Coder-7B-Instruct",
|
| 211 |
messages=messages,
|
| 212 |
max_tokens=2048,
|
| 213 |
temperature=0.7,
|
|
|
|
| 169 |
if not HF_TOKEN:
|
| 170 |
raise ValueError("HF_TOKEN is missing.")
|
| 171 |
|
| 172 |
+
# Initialize client with nscale router
|
| 173 |
client = InferenceClient(
|
| 174 |
+
base_url="https://router.huggingface.co/nscale/v1",
|
| 175 |
api_key=HF_TOKEN,
|
|
|
|
| 176 |
)
|
| 177 |
|
| 178 |
# System prompt
|
|
|
|
| 185 |
|
| 186 |
def chat_function(message, history):
|
| 187 |
|
| 188 |
+
messages = [
|
| 189 |
+
{
|
| 190 |
+
"role": "system",
|
| 191 |
+
"content": system_message
|
| 192 |
+
}
|
| 193 |
+
]
|
| 194 |
|
| 195 |
history = history or []
|
| 196 |
|
| 197 |
for item in history:
|
| 198 |
+
|
| 199 |
if isinstance(item, dict):
|
| 200 |
role = item.get("role")
|
| 201 |
content = item.get("content", "")
|
| 202 |
if role in ["user", "assistant"]:
|
| 203 |
+
messages.append({
|
| 204 |
+
"role": role,
|
| 205 |
+
"content": content
|
| 206 |
+
})
|
| 207 |
|
| 208 |
elif isinstance(item, (list, tuple)) and len(item) == 2:
|
| 209 |
user_msg, assistant_msg = item
|
| 210 |
if user_msg:
|
| 211 |
+
messages.append({
|
| 212 |
+
"role": "user",
|
| 213 |
+
"content": user_msg
|
| 214 |
+
})
|
| 215 |
if assistant_msg:
|
| 216 |
+
messages.append({
|
| 217 |
+
"role": "assistant",
|
| 218 |
+
"content": assistant_msg
|
| 219 |
+
})
|
| 220 |
|
| 221 |
+
messages.append({
|
| 222 |
+
"role": "user",
|
| 223 |
+
"content": message
|
| 224 |
+
})
|
| 225 |
|
| 226 |
try:
|
| 227 |
completion = client.chat.completions.create(
|
| 228 |
+
model="Qwen/Qwen2.5-Coder-7B-Instruct",
|
| 229 |
messages=messages,
|
| 230 |
max_tokens=2048,
|
| 231 |
temperature=0.7,
|