Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -239,71 +239,51 @@ def install_custom_packages(package_list):
|
|
239 |
|
240 |
@st.cache_resource(ttl=3600)
|
241 |
def init_ai_models(model_name=None):
|
242 |
-
try:
|
243 |
-
# Get the
|
244 |
-
|
245 |
-
if not
|
246 |
-
st.error("GitHub
|
247 |
return None
|
248 |
|
249 |
-
# Use default model
|
250 |
if not model_name:
|
251 |
model_name = "gpt-4o"
|
252 |
-
|
253 |
-
|
254 |
-
|
255 |
-
|
256 |
-
|
257 |
-
|
258 |
-
|
259 |
-
|
260 |
-
|
261 |
-
|
262 |
-
|
263 |
-
|
264 |
-
|
265 |
-
|
266 |
-
|
267 |
-
|
268 |
-
|
269 |
-
|
270 |
-
|
271 |
-
|
272 |
-
|
273 |
-
|
274 |
-
|
275 |
-
)
|
276 |
-
if not test_response or not test_response.choices:
|
277 |
-
st.error(f"API connection test failed for model {model_name}")
|
278 |
-
return None
|
279 |
-
except Exception as api_err:
|
280 |
-
st.error(f"API test failed: {str(api_err)}")
|
281 |
-
logger.error(f"API test error: {str(api_err)}")
|
282 |
-
return None
|
283 |
-
|
284 |
-
logger.info(f"Successfully initialized AI model: {model_name}")
|
285 |
-
return {
|
286 |
-
"client": client,
|
287 |
-
"model_name": model_name,
|
288 |
-
"last_loaded": datetime.now().isoformat()
|
289 |
-
}
|
290 |
-
except ImportError as ie:
|
291 |
-
st.error(f"Azure AI modules not available: {str(ie)}")
|
292 |
-
logger.error(f"Import error: {str(ie)}")
|
293 |
-
return None
|
294 |
except Exception as e:
|
295 |
-
st.error(f"Failed to
|
296 |
-
logger.error(f"AI model initialization error: {str(e)}")
|
297 |
return None
|
298 |
-
|
299 |
def suggest_code_completion(code_snippet, models):
|
300 |
if not models or "client" not in models:
|
301 |
st.error("AI models not properly initialized")
|
302 |
return None
|
303 |
-
|
304 |
try:
|
305 |
from azure.ai.inference.models import UserMessage
|
306 |
|
|
|
307 |
prompt = f"""Write a complete Manim animation scene based on this code or idea:
|
308 |
{code_snippet}
|
309 |
|
@@ -315,29 +295,26 @@ The code should be a complete, working Manim animation that includes:
|
|
315 |
|
316 |
Here's the complete Manim code:
|
317 |
"""
|
318 |
-
with st.spinner("AI is generating your animation code..."):
|
319 |
-
# Generate the response using Azure Inference API
|
320 |
-
response = models["client"].complete(
|
321 |
-
messages=[
|
322 |
-
UserMessage(prompt),
|
323 |
-
],
|
324 |
-
max_tokens=1024,
|
325 |
-
model=models["model_name"]
|
326 |
-
)
|
327 |
-
|
328 |
-
# Extract the generated code from the response
|
329 |
-
if response and response.choices and len(response.choices) > 0:
|
330 |
-
completed_code = response.choices[0].message.content
|
331 |
-
else:
|
332 |
-
st.error("No valid completion generated")
|
333 |
-
return None
|
334 |
-
|
335 |
-
# Extract code if wrapped in markdown code blocks
|
336 |
-
if "```python" in completed_code:
|
337 |
-
completed_code = completed_code.split("```python")[1].split("```")[0]
|
338 |
-
elif "```" in completed_code:
|
339 |
-
completed_code = completed_code.split("```")[1].split("```")[0]
|
340 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
341 |
if "Scene" not in completed_code:
|
342 |
completed_code = f"""from manim import *
|
343 |
|
@@ -347,8 +324,7 @@ class MyScene(Scene):
|
|
347 |
|
348 |
return completed_code
|
349 |
except Exception as e:
|
350 |
-
st.error(f"Error
|
351 |
-
logger.error(f"Code suggestion error: {str(e)}")
|
352 |
return None
|
353 |
|
354 |
def check_model_freshness():
|
|
|
239 |
|
240 |
@st.cache_resource(ttl=3600)
|
241 |
def init_ai_models(model_name=None):
|
242 |
+
try:
|
243 |
+
# Get the token directly from secrets
|
244 |
+
token = get_secret("github_token_api")
|
245 |
+
if not token:
|
246 |
+
st.error("GitHub token not found in secrets")
|
247 |
return None
|
248 |
|
249 |
+
# Use default model or specified model
|
250 |
if not model_name:
|
251 |
model_name = "gpt-4o"
|
252 |
+
|
253 |
+
# Import required modules
|
254 |
+
from azure.ai.inference import ChatCompletionsClient
|
255 |
+
from azure.ai.inference.models import SystemMessage, UserMessage
|
256 |
+
from azure.core.credentials import AzureKeyCredential
|
257 |
+
|
258 |
+
# Define endpoint
|
259 |
+
endpoint = "https://models.inference.ai.azure.com"
|
260 |
+
|
261 |
+
# Create client exactly as in your example
|
262 |
+
client = ChatCompletionsClient(
|
263 |
+
endpoint=endpoint,
|
264 |
+
credential=AzureKeyCredential(token),
|
265 |
+
)
|
266 |
+
|
267 |
+
# Store client and model info
|
268 |
+
return {
|
269 |
+
"client": client,
|
270 |
+
"model_name": model_name,
|
271 |
+
"endpoint": endpoint,
|
272 |
+
"token": token,
|
273 |
+
"last_loaded": datetime.now().isoformat()
|
274 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
275 |
except Exception as e:
|
276 |
+
st.error(f"Failed to initialize AI model: {str(e)}")
|
|
|
277 |
return None
|
|
|
278 |
def suggest_code_completion(code_snippet, models):
|
279 |
if not models or "client" not in models:
|
280 |
st.error("AI models not properly initialized")
|
281 |
return None
|
282 |
+
|
283 |
try:
|
284 |
from azure.ai.inference.models import UserMessage
|
285 |
|
286 |
+
# Create the prompt
|
287 |
prompt = f"""Write a complete Manim animation scene based on this code or idea:
|
288 |
{code_snippet}
|
289 |
|
|
|
295 |
|
296 |
Here's the complete Manim code:
|
297 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
298 |
|
299 |
+
# Make API call exactly like your example
|
300 |
+
response = models["client"].complete(
|
301 |
+
messages=[
|
302 |
+
UserMessage(prompt),
|
303 |
+
],
|
304 |
+
max_tokens=1024,
|
305 |
+
model=models["model_name"]
|
306 |
+
)
|
307 |
+
|
308 |
+
# Get response content exactly like your example
|
309 |
+
completed_code = response.choices[0].message.content
|
310 |
+
|
311 |
+
# Clean up the code if it's wrapped in markdown blocks
|
312 |
+
if "```python" in completed_code:
|
313 |
+
completed_code = completed_code.split("```python")[1].split("```")[0]
|
314 |
+
elif "```" in completed_code:
|
315 |
+
completed_code = completed_code.split("```")[1].split("```")[0]
|
316 |
+
|
317 |
+
# Add Scene class if missing
|
318 |
if "Scene" not in completed_code:
|
319 |
completed_code = f"""from manim import *
|
320 |
|
|
|
324 |
|
325 |
return completed_code
|
326 |
except Exception as e:
|
327 |
+
st.error(f"Error generating code: {str(e)}")
|
|
|
328 |
return None
|
329 |
|
330 |
def check_model_freshness():
|