Spaces:
Running
Running
use hf fireworks for qwen
Browse files
app.py
CHANGED
|
@@ -236,8 +236,8 @@ AVAILABLE_MODELS = [
|
|
| 236 |
},
|
| 237 |
{
|
| 238 |
"name": "Qwen3-235B-A22B-Instruct-2507",
|
| 239 |
-
"id": "
|
| 240 |
-
"description": "Qwen3-235B-A22B model
|
| 241 |
}
|
| 242 |
]
|
| 243 |
|
|
@@ -309,8 +309,6 @@ def get_inference_client(model_id, provider="auto"):
|
|
| 309 |
"""Return an InferenceClient with provider based on model_id and user selection."""
|
| 310 |
if model_id == "moonshotai/Kimi-K2-Instruct":
|
| 311 |
provider = "groq"
|
| 312 |
-
if model_id == "openrouter/qwen3-235b-a22b-07-25:free":
|
| 313 |
-
return "openrouter"
|
| 314 |
return InferenceClient(
|
| 315 |
provider=provider,
|
| 316 |
api_key=HF_TOKEN,
|
|
@@ -1157,73 +1155,6 @@ This will help me create a better design for you."""
|
|
| 1157 |
# Use dynamic client based on selected model
|
| 1158 |
client = get_inference_client(_current_model["id"], provider)
|
| 1159 |
|
| 1160 |
-
# --- FIX: Handle OpenRouter client before HuggingFace logic ---
|
| 1161 |
-
if client == "openrouter":
|
| 1162 |
-
import os
|
| 1163 |
-
from openai import OpenAI
|
| 1164 |
-
openrouter_api_key = os.getenv("OPENROUTER_API_KEY")
|
| 1165 |
-
openrouter_site_url = os.getenv("OPENROUTER_SITE_URL", "https://huggingface.co/spaces/akhaliq/anycoder")
|
| 1166 |
-
openrouter_site_title = os.getenv("OPENROUTER_SITE_TITLE", "AnyCoder")
|
| 1167 |
-
if not openrouter_api_key:
|
| 1168 |
-
error_message = "Error: OPENROUTER_API_KEY environment variable is not set."
|
| 1169 |
-
yield {
|
| 1170 |
-
code_output: error_message,
|
| 1171 |
-
history_output: history_to_chatbot_messages(_history),
|
| 1172 |
-
}
|
| 1173 |
-
return
|
| 1174 |
-
openai_client = OpenAI(
|
| 1175 |
-
base_url="https://openrouter.ai/api/v1",
|
| 1176 |
-
api_key=openrouter_api_key,
|
| 1177 |
-
)
|
| 1178 |
-
# Prepare OpenAI message format
|
| 1179 |
-
openai_messages = []
|
| 1180 |
-
for m in messages:
|
| 1181 |
-
if m["role"] == "system":
|
| 1182 |
-
openai_messages.append({"role": "system", "content": m["content"]})
|
| 1183 |
-
elif m["role"] == "user":
|
| 1184 |
-
openai_messages.append({"role": "user", "content": m["content"]})
|
| 1185 |
-
elif m["role"] == "assistant":
|
| 1186 |
-
openai_messages.append({"role": "assistant", "content": m["content"]})
|
| 1187 |
-
openai_messages.append({"role": "user", "content": enhanced_query})
|
| 1188 |
-
try:
|
| 1189 |
-
completion = openai_client.chat.completions.create(
|
| 1190 |
-
model="qwen/qwen3-235b-a22b-07-25:free",
|
| 1191 |
-
messages=openai_messages,
|
| 1192 |
-
extra_headers={
|
| 1193 |
-
"HTTP-Referer": openrouter_site_url,
|
| 1194 |
-
"X-Title": openrouter_site_title,
|
| 1195 |
-
},
|
| 1196 |
-
extra_body={},
|
| 1197 |
-
stream=True,
|
| 1198 |
-
max_tokens=10000
|
| 1199 |
-
)
|
| 1200 |
-
content = ""
|
| 1201 |
-
for chunk in completion:
|
| 1202 |
-
if hasattr(chunk, "choices") and chunk.choices and hasattr(chunk.choices[0], "delta") and hasattr(chunk.choices[0].delta, "content") and chunk.choices[0].delta.content is not None:
|
| 1203 |
-
content += chunk.choices[0].delta.content
|
| 1204 |
-
clean_code = remove_code_block(content)
|
| 1205 |
-
yield {
|
| 1206 |
-
code_output: gr.update(value=clean_code, language=get_gradio_language(language)),
|
| 1207 |
-
history_output: history_to_chatbot_messages(_history),
|
| 1208 |
-
sandbox: send_to_sandbox(clean_code) if language == "html" else "<div style='padding:1em;color:#888;text-align:center;'>Preview is only available for HTML. Please download your code using the download button above.</div>",
|
| 1209 |
-
}
|
| 1210 |
-
# After streaming, update history
|
| 1211 |
-
_history.append([query, content])
|
| 1212 |
-
yield {
|
| 1213 |
-
code_output: remove_code_block(content),
|
| 1214 |
-
history: _history,
|
| 1215 |
-
sandbox: send_to_sandbox(remove_code_block(content)),
|
| 1216 |
-
history_output: history_to_chatbot_messages(_history),
|
| 1217 |
-
}
|
| 1218 |
-
except Exception as e:
|
| 1219 |
-
error_message = f"Error (OpenRouter): {str(e)}"
|
| 1220 |
-
yield {
|
| 1221 |
-
code_output: error_message,
|
| 1222 |
-
history_output: history_to_chatbot_messages(_history),
|
| 1223 |
-
}
|
| 1224 |
-
return
|
| 1225 |
-
# --- END FIX ---
|
| 1226 |
-
|
| 1227 |
if image is not None:
|
| 1228 |
messages.append(create_multimodal_message(enhanced_query, image))
|
| 1229 |
else:
|
|
|
|
| 236 |
},
|
| 237 |
{
|
| 238 |
"name": "Qwen3-235B-A22B-Instruct-2507",
|
| 239 |
+
"id": "Qwen/Qwen3-235B-A22B-Instruct-2507",
|
| 240 |
+
"description": "Qwen3-235B-A22B-Instruct-2507 model for code generation and general tasks"
|
| 241 |
}
|
| 242 |
]
|
| 243 |
|
|
|
|
| 309 |
"""Return an InferenceClient with provider based on model_id and user selection."""
|
| 310 |
if model_id == "moonshotai/Kimi-K2-Instruct":
|
| 311 |
provider = "groq"
|
|
|
|
|
|
|
| 312 |
return InferenceClient(
|
| 313 |
provider=provider,
|
| 314 |
api_key=HF_TOKEN,
|
|
|
|
| 1155 |
# Use dynamic client based on selected model
|
| 1156 |
client = get_inference_client(_current_model["id"], provider)
|
| 1157 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1158 |
if image is not None:
|
| 1159 |
messages.append(create_multimodal_message(enhanced_query, image))
|
| 1160 |
else:
|