Spaces:
Running
Running
| import os | |
| import requests | |
| import gradio as gr | |
| from datetime import datetime | |
| # API Configuration | |
| API_KEY = os.getenv('StableCogKey') | |
| API_HOST = 'https://api.stablecog.com' | |
| headers = { | |
| 'Authorization': f'Bearer {API_KEY}', | |
| 'Content-Type': 'application/json' | |
| } | |
| def get_models(): | |
| """Fetch and display available models""" | |
| try: | |
| url = f'{API_HOST}/v1/image/generation/models' | |
| response = requests.get(url, headers=headers, timeout=10) | |
| if response.status_code == 200: | |
| data = response.json() | |
| models = data.get('models', []) | |
| # Format display | |
| display_text = f"📊 Found {len(models)} models\n" | |
| display_text += f"⏰ {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}\n\n" | |
| for i, model in enumerate(models, 1): | |
| name = model.get('name', 'Unknown') | |
| model_type = model.get('type', 'unknown') | |
| description = model.get('description', 'No description') | |
| display_text += f"{i}. 🔹 **{name}**\n" | |
| display_text += f" 📝 {description}\n" | |
| display_text += f" 🏷️ Type: {model_type}\n" | |
| display_text += f" 🌍 Public: {'✅' if model.get('is_public') else '❌'}\n" | |
| display_text += f" ⭐ Default: {'✅' if model.get('is_default') else '❌'}\n" | |
| display_text += f" 👥 Community: {'✅' if model.get('is_community') else '❌'}\n" | |
| display_text += "─" * 40 + "\n" | |
| return display_text, str(data) | |
| else: | |
| return f"❌ Error {response.status_code}", f"Error: {response.text}" | |
| except Exception as e: | |
| return f"❌ Error: {str(e)}", "No data" | |
| def get_outputs(): | |
| """Fetch and display recent outputs""" | |
| try: | |
| url = f'{API_HOST}/v1/image/generation/outputs' | |
| response = requests.get(url, headers=headers, timeout=10) | |
| if response.status_code == 200: | |
| data = response.json() | |
| outputs = data.get('outputs', []) | |
| total = data.get('total_count', 0) | |
| next_cursor = data.get('next', 'None') | |
| # Format display | |
| display_text = f"📊 Total outputs: {total}\n" | |
| display_text += f"📋 Showing: {len(outputs)} outputs\n" | |
| display_text += f"⏭️ Next cursor: {next_cursor}\n" | |
| display_text += f"⏰ {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}\n\n" | |
| for i, output in enumerate(outputs, 1): | |
| output_id = output.get('id', 'N/A')[:8] + '...' | |
| created_at = output.get('created_at', 'N/A') | |
| status = output.get('status', 'unknown') | |
| model_name = output.get('model_name', 'Unknown') | |
| # Format timestamp | |
| if created_at != 'N/A': | |
| try: | |
| dt = datetime.fromisoformat(created_at.replace('Z', '+00:00')) | |
| created_at = dt.strftime('%Y-%m-%d %H:%M') | |
| except: | |
| pass | |
| display_text += f"{i}. 🔹 **Output {output_id}**\n" | |
| display_text += f" 🕒 Created: {created_at}\n" | |
| display_text += f" 📊 Status: {status}\n" | |
| display_text += f" 🤖 Model: {model_name}\n" | |
| # Show image URLs if available | |
| images = output.get('image_urls', []) | |
| if images: | |
| display_text += f" 🖼️ Images: {len(images)}\n" | |
| for img_url in images[:2]: # Show first 2 URLs | |
| display_text += f" 🔗 {img_url[:50]}...\n" | |
| display_text += "─" * 40 + "\n" | |
| return display_text, str(data) | |
| else: | |
| return f"❌ Error {response.status_code}", f"Error: {response.text}" | |
| except Exception as e: | |
| return f"❌ Error: {str(e)}", "No data" | |
| # Create Gradio interface | |
| with gr.Blocks(title="StableCog Dashboard") as demo: | |
| gr.Markdown("# 🎯 StableCog Dashboard") | |
| with gr.Tabs(): | |
| with gr.Tab("🤖 Models"): | |
| with gr.Row(): | |
| models_display = gr.Textbox(label="Available Models", lines=25) | |
| models_raw = gr.Code(label="Raw JSON", language="json", lines=25) | |
| check_models_btn = gr.Button("🔄 Refresh Models", variant="primary") | |
| check_models_btn.click(get_models, outputs=[models_display, models_raw]) | |
| with gr.Tab("🖼️ Outputs"): | |
| with gr.Row(): | |
| outputs_display = gr.Textbox(label="Recent Outputs", lines=25) | |
| outputs_raw = gr.Code(label="Raw JSON", language="json", lines=25) | |
| check_outputs_btn = gr.Button("🔄 Refresh Outputs", variant="primary") | |
| check_outputs_btn.click(get_outputs, outputs=[outputs_display, outputs_raw]) | |
| if __name__ == "__main__": | |
| demo.launch() |