Update app.py
Browse files
app.py
CHANGED
@@ -229,8 +229,7 @@ def get_inference_client(model_id):
|
|
229 |
if provider == "deepseek":
|
230 |
if not DEEPSEEK_API_KEY:
|
231 |
raise ValueError("DEEPSEEK_API_KEY environment variable not set.")
|
232 |
-
|
233 |
-
return DeepSeekClient(api_key=DEEPSEEK_API_KEY)
|
234 |
|
235 |
if provider == "groq":
|
236 |
return InferenceClient(
|
@@ -1087,25 +1086,38 @@ This will help me create a better design for you."""
|
|
1087 |
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>",
|
1088 |
}
|
1089 |
elif _current_model["provider"] == "deepseek":
|
1090 |
-
# Use Deepseek
|
1091 |
-
|
1092 |
-
|
1093 |
-
|
1094 |
-
|
1095 |
-
|
1096 |
-
|
1097 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1098 |
content = ""
|
1099 |
-
for
|
1100 |
-
if
|
1101 |
-
|
1102 |
-
|
1103 |
-
|
1104 |
-
|
1105 |
-
|
1106 |
-
|
1107 |
-
|
1108 |
-
|
|
|
|
|
|
|
|
|
|
|
1109 |
|
1110 |
else:
|
1111 |
# Use Hugging Face Inference Client (or Groq) for other models
|
|
|
229 |
if provider == "deepseek":
|
230 |
if not DEEPSEEK_API_KEY:
|
231 |
raise ValueError("DEEPSEEK_API_KEY environment variable not set.")
|
232 |
+
return "deepseek_client" # Return a string placeholder, logic is handled in generation_code
|
|
|
233 |
|
234 |
if provider == "groq":
|
235 |
return InferenceClient(
|
|
|
1086 |
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>",
|
1087 |
}
|
1088 |
elif _current_model["provider"] == "deepseek":
|
1089 |
+
# Use requests to call Deepseek API directly
|
1090 |
+
url = "https://api.deepseek.com/v1/chat/completions"
|
1091 |
+
headers = {
|
1092 |
+
"Authorization": f"Bearer {DEEPSEEK_API_KEY}",
|
1093 |
+
"Content-Type": "application/json"
|
1094 |
+
}
|
1095 |
+
payload = {
|
1096 |
+
"model": _current_model["id"].split('/')[-1], # e.g., "DeepSeek-V3-0324" -> "DeepSeek-V3-0324"
|
1097 |
+
"messages": [{"role": m["role"], "content": m["content"]} for m in messages],
|
1098 |
+
"stream": True,
|
1099 |
+
"max_tokens": 5000
|
1100 |
+
}
|
1101 |
+
|
1102 |
+
response = requests.post(url, headers=headers, json=payload, stream=True)
|
1103 |
+
response.raise_for_status()
|
1104 |
+
|
1105 |
content = ""
|
1106 |
+
for line in response.iter_lines():
|
1107 |
+
if line:
|
1108 |
+
line_str = line.decode('utf-8')
|
1109 |
+
if line_str.startswith('data: '):
|
1110 |
+
json_data = line_str[6:]
|
1111 |
+
if json_data.strip() != '[DONE]':
|
1112 |
+
chunk = json.loads(json_data)
|
1113 |
+
if chunk['choices'][0]['delta'].get('content'):
|
1114 |
+
content += chunk['choices'][0]['delta']['content']
|
1115 |
+
clean_code = remove_code_block(content)
|
1116 |
+
yield {
|
1117 |
+
code_output: gr.update(value=clean_code, language=get_gradio_language(language)),
|
1118 |
+
history_output: history_to_chatbot_messages(_history),
|
1119 |
+
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>",
|
1120 |
+
}
|
1121 |
|
1122 |
else:
|
1123 |
# Use Hugging Face Inference Client (or Groq) for other models
|