Spaces:
Sleeping
Sleeping
| import os | |
| import json | |
| import base64 | |
| import requests | |
| OPENROUTER_API_KEY = "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" | |
| MODEL = "google/gemini-2.5-flash" | |
| API_URL = "https://openrouter.ai/api/v1/chat/completions" | |
| def analyze_image(image_path: str): | |
| if not image_path: | |
| return { | |
| "success": False, | |
| "description": "No image provided." | |
| } | |
| if not os.path.exists(image_path): | |
| return { | |
| "success": False, | |
| "description": f"Image not found: {image_path}" | |
| } | |
| with open(image_path, "rb") as f: | |
| image = base64.b64encode(f.read()).decode("utf-8") | |
| headers = { | |
| "Authorization": f"Bearer {OPENROUTER_API_KEY}", | |
| "Content-Type": "application/json", | |
| "HTTP-Referer": "http://localhost", | |
| "X-Title": "AI Image Studio" | |
| } | |
| prompt = """ | |
| You are an expert computer vision assistant. | |
| Analyze this image. | |
| Return ONLY valid JSON. | |
| { | |
| "description":"", | |
| "objects":[], | |
| "people":"", | |
| "style":"", | |
| "lighting":"", | |
| "colors":[], | |
| "background":"", | |
| "camera_angle":"", | |
| "quality":"", | |
| "editing_prompt":"", | |
| "suggestions":[] | |
| } | |
| Rules: | |
| Return JSON only. | |
| Do not write markdown. | |
| Do not use ```. | |
| editing_prompt should preserve every important detail so future editing keeps the same image. | |
| suggestions should contain 6 editing ideas. | |
| """ | |
| payload = { | |
| "model": MODEL, | |
| "messages": [ | |
| { | |
| "role": "user", | |
| "content": [ | |
| { | |
| "type": "text", | |
| "text": prompt | |
| }, | |
| { | |
| "type": "image_url", | |
| "image_url": { | |
| "url": f"data:image/jpeg;base64,{image}" | |
| } | |
| } | |
| ] | |
| } | |
| ], | |
| "temperature": 0.2, | |
| "max_tokens": 1500 | |
| } | |
| try: | |
| response = requests.post( | |
| API_URL, | |
| headers=headers, | |
| json=payload, | |
| timeout=180 | |
| ) | |
| print("Status:", response.status_code) | |
| result = response.json() | |
| print(result) | |
| if response.status_code != 200: | |
| return { | |
| "success": False, | |
| "description": result.get( | |
| "error", | |
| {} | |
| ).get( | |
| "message", | |
| "Unknown OpenRouter error." | |
| ) | |
| } | |
| if "choices" not in result: | |
| return { | |
| "success": False, | |
| "description": "No choices returned.", | |
| "raw": result | |
| } | |
| content = result["choices"][0]["message"]["content"] | |
| content = content.replace("```json", "") | |
| content = content.replace("```", "") | |
| content = content.strip() | |
| try: | |
| analysis = json.loads(content) | |
| analysis["success"] = True | |
| return analysis | |
| except Exception: | |
| return { | |
| "success": True, | |
| "description": content, | |
| "objects": [], | |
| "people": "", | |
| "style": "", | |
| "lighting": "", | |
| "colors": [], | |
| "background": "", | |
| "camera_angle": "", | |
| "quality": "", | |
| "editing_prompt": content, | |
| "suggestions": [ | |
| "Make it Anime", | |
| "Pixar Style", | |
| "Remove Background", | |
| "Replace Background", | |
| "Enhance Quality", | |
| "Change Colors" | |
| ] | |
| } | |
| except Exception as e: | |
| return { | |
| "success": False, | |
| "description": str(e) | |
| } |