Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,12 +1,11 @@
|
|
| 1 |
import os
|
| 2 |
import requests
|
| 3 |
import gradio as gr
|
|
|
|
| 4 |
|
| 5 |
# API Configuration
|
| 6 |
API_KEY = os.getenv('StableCogKey')
|
| 7 |
API_HOST = 'https://api.stablecog.com'
|
| 8 |
-
MODELS_ENDPOINT = '/v1/image/generation/models'
|
| 9 |
-
URL = f'{API_HOST}{MODELS_ENDPOINT}'
|
| 10 |
|
| 11 |
headers = {
|
| 12 |
'Authorization': f'Bearer {API_KEY}',
|
|
@@ -16,47 +15,178 @@ headers = {
|
|
| 16 |
def get_models():
|
| 17 |
"""Fetch and display available models"""
|
| 18 |
try:
|
| 19 |
-
|
|
|
|
| 20 |
|
| 21 |
if response.status_code == 200:
|
| 22 |
data = response.json()
|
| 23 |
models = data.get('models', [])
|
| 24 |
|
| 25 |
# Format display
|
| 26 |
-
display_text = ""
|
| 27 |
-
|
|
|
|
|
|
|
| 28 |
name = model.get('name', 'Unknown')
|
| 29 |
model_type = model.get('type', 'unknown')
|
| 30 |
description = model.get('description', 'No description')
|
| 31 |
|
| 32 |
-
display_text += f"🔹 {name}
|
| 33 |
display_text += f" 📝 {description}\n"
|
| 34 |
-
display_text += f"
|
| 35 |
-
display_text += f"
|
| 36 |
-
display_text += f"
|
|
|
|
| 37 |
display_text += "─" * 40 + "\n"
|
| 38 |
|
| 39 |
-
total = len(models)
|
| 40 |
-
display_text = f"📊 Found {total} models:\n\n" + display_text
|
| 41 |
-
|
| 42 |
return display_text, str(data)
|
| 43 |
|
| 44 |
else:
|
| 45 |
-
return f"❌ Error
|
| 46 |
|
| 47 |
except Exception as e:
|
| 48 |
-
return f"❌
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
|
| 50 |
# Create Gradio interface
|
| 51 |
-
with gr.Blocks(title="StableCog
|
| 52 |
-
gr.Markdown("#
|
| 53 |
|
| 54 |
-
with gr.
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
|
| 61 |
if __name__ == "__main__":
|
| 62 |
demo.launch()
|
|
|
|
| 1 |
import os
|
| 2 |
import requests
|
| 3 |
import gradio as gr
|
| 4 |
+
from datetime import datetime
|
| 5 |
|
| 6 |
# API Configuration
|
| 7 |
API_KEY = os.getenv('StableCogKey')
|
| 8 |
API_HOST = 'https://api.stablecog.com'
|
|
|
|
|
|
|
| 9 |
|
| 10 |
headers = {
|
| 11 |
'Authorization': f'Bearer {API_KEY}',
|
|
|
|
| 15 |
def get_models():
|
| 16 |
"""Fetch and display available models"""
|
| 17 |
try:
|
| 18 |
+
url = f'{API_HOST}/v1/image/generation/models'
|
| 19 |
+
response = requests.get(url, headers=headers, timeout=10)
|
| 20 |
|
| 21 |
if response.status_code == 200:
|
| 22 |
data = response.json()
|
| 23 |
models = data.get('models', [])
|
| 24 |
|
| 25 |
# Format display
|
| 26 |
+
display_text = f"📊 Found {len(models)} models\n"
|
| 27 |
+
display_text += f"⏰ {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}\n\n"
|
| 28 |
+
|
| 29 |
+
for i, model in enumerate(models, 1):
|
| 30 |
name = model.get('name', 'Unknown')
|
| 31 |
model_type = model.get('type', 'unknown')
|
| 32 |
description = model.get('description', 'No description')
|
| 33 |
|
| 34 |
+
display_text += f"{i}. 🔹 **{name}**\n"
|
| 35 |
display_text += f" 📝 {description}\n"
|
| 36 |
+
display_text += f" 🏷️ Type: {model_type}\n"
|
| 37 |
+
display_text += f" 🌍 Public: {'✅' if model.get('is_public') else '❌'}\n"
|
| 38 |
+
display_text += f" ⭐ Default: {'✅' if model.get('is_default') else '❌'}\n"
|
| 39 |
+
display_text += f" 👥 Community: {'✅' if model.get('is_community') else '❌'}\n"
|
| 40 |
display_text += "─" * 40 + "\n"
|
| 41 |
|
|
|
|
|
|
|
|
|
|
| 42 |
return display_text, str(data)
|
| 43 |
|
| 44 |
else:
|
| 45 |
+
return f"❌ Error {response.status_code}", f"Error: {response.text}"
|
| 46 |
|
| 47 |
except Exception as e:
|
| 48 |
+
return f"❌ Error: {str(e)}", "No data"
|
| 49 |
+
|
| 50 |
+
def get_outputs():
|
| 51 |
+
"""Fetch and display recent outputs with images"""
|
| 52 |
+
try:
|
| 53 |
+
url = f'{API_HOST}/v1/image/generation/outputs'
|
| 54 |
+
response = requests.get(url, headers=headers, timeout=10)
|
| 55 |
+
|
| 56 |
+
if response.status_code == 200:
|
| 57 |
+
data = response.json()
|
| 58 |
+
outputs = data.get('outputs', [])
|
| 59 |
+
total = data.get('total_count', 0)
|
| 60 |
+
next_cursor = data.get('next', 'None')
|
| 61 |
+
|
| 62 |
+
# Prepare display components
|
| 63 |
+
image_gallery = []
|
| 64 |
+
display_text = ""
|
| 65 |
+
gallery_html = "<div style='display: grid; grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); gap: 15px;'>"
|
| 66 |
+
|
| 67 |
+
if outputs:
|
| 68 |
+
display_text += f"📊 Total outputs: {total}\n"
|
| 69 |
+
display_text += f"📋 Showing: {len(outputs)} outputs\n"
|
| 70 |
+
display_text += f"⏭️ Next cursor: {next_cursor}\n"
|
| 71 |
+
display_text += f"⏰ {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}\n\n"
|
| 72 |
+
|
| 73 |
+
for i, output in enumerate(outputs, 1):
|
| 74 |
+
output_id = output.get('id', 'N/A')
|
| 75 |
+
short_id = output_id[:8] + '...' if len(output_id) > 8 else output_id
|
| 76 |
+
created_at = output.get('created_at', 'N/A')
|
| 77 |
+
status = output.get('status', 'unknown')
|
| 78 |
+
model_name = output.get('model_name', 'Unknown')
|
| 79 |
+
|
| 80 |
+
# Gallery status with emojis
|
| 81 |
+
gallery_status = output.get('gallery_status', 'not_submitted')
|
| 82 |
+
gallery_emoji = {
|
| 83 |
+
'not_submitted': '🔒',
|
| 84 |
+
'submitted': '📤',
|
| 85 |
+
'approved': '✅',
|
| 86 |
+
'rejected': '❌'
|
| 87 |
+
}.get(gallery_status, '❓')
|
| 88 |
+
|
| 89 |
+
# Favorites
|
| 90 |
+
is_favorited = output.get('is_favorited', False)
|
| 91 |
+
favorite_emoji = '❤️' if is_favorited else '🤍'
|
| 92 |
+
|
| 93 |
+
# Auto-submitted
|
| 94 |
+
was_auto_submitted = output.get('was_auto_submitted', False)
|
| 95 |
+
auto_submit_emoji = '🤖' if was_auto_submitted else '👤'
|
| 96 |
+
|
| 97 |
+
# Format timestamp
|
| 98 |
+
if created_at != 'N/A':
|
| 99 |
+
try:
|
| 100 |
+
dt = datetime.fromisoformat(created_at.replace('Z', '+00:00'))
|
| 101 |
+
created_at = dt.strftime('%Y-%m-%d %H:%M')
|
| 102 |
+
except:
|
| 103 |
+
pass
|
| 104 |
+
|
| 105 |
+
display_text += f"{i}. 🔹 **Output {short_id}**\n"
|
| 106 |
+
display_text += f" 🕒 Created: {created_at}\n"
|
| 107 |
+
display_text += f" 📊 Status: {status}\n"
|
| 108 |
+
display_text += f" 🤖 Model: {model_name}\n"
|
| 109 |
+
display_text += f" 🖼️ Gallery: {gallery_emoji} {gallery_status}\n"
|
| 110 |
+
display_text += f" {favorite_emoji} Favorite\n"
|
| 111 |
+
display_text += f" {auto_submit_emoji} {'Auto-submitted' if was_auto_submitted else 'Manual'}\n"
|
| 112 |
+
|
| 113 |
+
# Get image URLs
|
| 114 |
+
image_url = output.get('image_url')
|
| 115 |
+
image_urls = output.get('image_urls', [])
|
| 116 |
+
|
| 117 |
+
# Use image_url if available, otherwise check image_urls array
|
| 118 |
+
image_to_show = image_url or (image_urls[0] if image_urls else None)
|
| 119 |
+
|
| 120 |
+
if image_to_show:
|
| 121 |
+
# Add to gallery
|
| 122 |
+
gallery_html += f"""
|
| 123 |
+
<div style='border-radius: 10px; overflow: hidden; background: rgba(255,255,255,0.1); padding: 10px;'>
|
| 124 |
+
<img src='{image_to_show}' style='width: 100%; height: 200px; object-fit: cover; border-radius: 8px;'>
|
| 125 |
+
<div style='margin-top: 8px; font-size: 12px;'>
|
| 126 |
+
<div>📅 {created_at.split()[0] if ' ' in created_at else created_at}</div>
|
| 127 |
+
<div>🤖 {model_name[:15]}{'...' if len(model_name) > 15 else ''}</div>
|
| 128 |
+
<div>{gallery_emoji} {gallery_status}</div>
|
| 129 |
+
<div>{favorite_emoji}</div>
|
| 130 |
+
</div>
|
| 131 |
+
</div>
|
| 132 |
+
"""
|
| 133 |
+
|
| 134 |
+
display_text += f" 🖼️ Image: {image_to_show[:50]}...\n"
|
| 135 |
+
else:
|
| 136 |
+
display_text += f" 🖼️ No image available\n"
|
| 137 |
+
|
| 138 |
+
# Show generation details if available
|
| 139 |
+
generation = output.get('generation', {})
|
| 140 |
+
if generation:
|
| 141 |
+
prompt = generation.get('prompt', 'No prompt')
|
| 142 |
+
width = generation.get('width', '?')
|
| 143 |
+
height = generation.get('height', '?')
|
| 144 |
+
|
| 145 |
+
display_text += f" 📝 Prompt: {prompt[:50]}...\n"
|
| 146 |
+
display_text += f" 📐 Size: {width}x{height}\n"
|
| 147 |
+
|
| 148 |
+
display_text += "─" * 40 + "\n"
|
| 149 |
+
else:
|
| 150 |
+
display_text = "📭 No outputs found. Generate some images first!"
|
| 151 |
+
gallery_html += "<div style='text-align: center; padding: 40px;'>No images found</div>"
|
| 152 |
+
|
| 153 |
+
gallery_html += "</div>"
|
| 154 |
+
|
| 155 |
+
return display_text, gallery_html, str(data)
|
| 156 |
+
|
| 157 |
+
else:
|
| 158 |
+
error_text = f"❌ Error {response.status_code}"
|
| 159 |
+
return error_text, "<div style='color: red; padding: 20px;'>" + error_text + "</div>", f"Error: {response.text}"
|
| 160 |
+
|
| 161 |
+
except Exception as e:
|
| 162 |
+
error_text = f"❌ Error: {str(e)}"
|
| 163 |
+
return error_text, "<div style='color: red; padding: 20px;'>" + error_text + "</div>", "No data"
|
| 164 |
|
| 165 |
# Create Gradio interface
|
| 166 |
+
with gr.Blocks(title="StableCog Dashboard") as demo:
|
| 167 |
+
gr.Markdown("# 🎯 StableCog Dashboard")
|
| 168 |
|
| 169 |
+
with gr.Tabs():
|
| 170 |
+
with gr.Tab("🤖 Models"):
|
| 171 |
+
with gr.Row():
|
| 172 |
+
models_display = gr.Textbox(label="Available Models", lines=25)
|
| 173 |
+
models_raw = gr.Code(label="Raw JSON", language="json", lines=25)
|
| 174 |
+
|
| 175 |
+
check_models_btn = gr.Button("🔄 Refresh Models", variant="primary")
|
| 176 |
+
check_models_btn.click(get_models, outputs=[models_display, models_raw])
|
| 177 |
+
|
| 178 |
+
with gr.Tab("🖼️ Outputs"):
|
| 179 |
+
with gr.Row():
|
| 180 |
+
with gr.Column(scale=2):
|
| 181 |
+
outputs_display = gr.Textbox(label="Output Details", lines=25)
|
| 182 |
+
with gr.Column(scale=3):
|
| 183 |
+
outputs_gallery = gr.HTML(label="Image Gallery")
|
| 184 |
+
|
| 185 |
+
with gr.Row():
|
| 186 |
+
outputs_raw = gr.Code(label="Raw JSON", language="json", lines=15)
|
| 187 |
+
|
| 188 |
+
check_outputs_btn = gr.Button("🔄 Refresh Outputs", variant="primary")
|
| 189 |
+
check_outputs_btn.click(get_outputs, outputs=[outputs_display, outputs_gallery, outputs_raw])
|
| 190 |
|
| 191 |
if __name__ == "__main__":
|
| 192 |
demo.launch()
|