Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -130,7 +130,7 @@ def generate_image(prompt, project_id=None, scene_num=1):
|
|
| 130 |
raise
|
| 131 |
|
| 132 |
# =============================================
|
| 133 |
-
# API ENDPOINTS
|
| 134 |
# =============================================
|
| 135 |
class GenerateRequest(BaseModel):
|
| 136 |
prompt: str
|
|
@@ -158,43 +158,58 @@ async def health():
|
|
| 158 |
"hf_dataset": DATASET_ID if HF_TOKEN else "disabled"
|
| 159 |
}
|
| 160 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 161 |
# =============================================
|
| 162 |
-
# GRADIO INTERFACE (
|
| 163 |
# =============================================
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 185 |
|
| 186 |
# =============================================
|
| 187 |
-
# MAIN
|
| 188 |
# =============================================
|
| 189 |
if __name__ == "__main__":
|
| 190 |
import uvicorn
|
| 191 |
print("\n" + "=" * 60)
|
| 192 |
print("π Server starting on port 7860")
|
| 193 |
print("π API endpoints:")
|
| 194 |
-
print(" -
|
| 195 |
print(" - GET /health")
|
| 196 |
-
print(" -
|
|
|
|
|
|
|
| 197 |
print("=" * 60)
|
| 198 |
|
| 199 |
-
# Make sure app is properly configured
|
| 200 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|
|
| 130 |
raise
|
| 131 |
|
| 132 |
# =============================================
|
| 133 |
+
# API ENDPOINTS (Define these BEFORE mounting Gradio)
|
| 134 |
# =============================================
|
| 135 |
class GenerateRequest(BaseModel):
|
| 136 |
prompt: str
|
|
|
|
| 158 |
"hf_dataset": DATASET_ID if HF_TOKEN else "disabled"
|
| 159 |
}
|
| 160 |
|
| 161 |
+
@app.get("/api-test")
|
| 162 |
+
async def api_test():
|
| 163 |
+
"""Simple test endpoint to verify API is working"""
|
| 164 |
+
return {"message": "API is working!", "endpoints": ["/health", "/generate", "/api-test"]}
|
| 165 |
+
|
| 166 |
# =============================================
|
| 167 |
+
# GRADIO INTERFACE (Mounted at /ui instead of root)
|
| 168 |
# =============================================
|
| 169 |
+
def gradio_generate(prompt):
|
| 170 |
+
if not prompt:
|
| 171 |
+
return None
|
| 172 |
+
result = generate_image(prompt)
|
| 173 |
+
return result["image"]
|
| 174 |
+
|
| 175 |
+
iface = gr.Interface(
|
| 176 |
+
fn=gradio_generate,
|
| 177 |
+
inputs=gr.Textbox(label="Prompt", placeholder="Enter your prompt..."),
|
| 178 |
+
outputs=gr.Image(label="Generated Image"),
|
| 179 |
+
title="Image Generator",
|
| 180 |
+
description="Generate images with Stable Diffusion"
|
| 181 |
+
)
|
| 182 |
+
|
| 183 |
+
# Mount Gradio at /ui path instead of root
|
| 184 |
+
gr.mount_gradio_app(app, iface, path="/ui")
|
| 185 |
+
|
| 186 |
+
# Root endpoint returns API info
|
| 187 |
+
@app.get("/")
|
| 188 |
+
async def root():
|
| 189 |
+
return {
|
| 190 |
+
"message": "Image Generator API",
|
| 191 |
+
"endpoints": {
|
| 192 |
+
"health": "GET /health",
|
| 193 |
+
"generate": "POST /generate",
|
| 194 |
+
"api_test": "GET /api-test",
|
| 195 |
+
"ui": "GET /ui (Gradio interface)"
|
| 196 |
+
},
|
| 197 |
+
"status": "running"
|
| 198 |
+
}
|
| 199 |
|
| 200 |
# =============================================
|
| 201 |
+
# MAIN
|
| 202 |
# =============================================
|
| 203 |
if __name__ == "__main__":
|
| 204 |
import uvicorn
|
| 205 |
print("\n" + "=" * 60)
|
| 206 |
print("π Server starting on port 7860")
|
| 207 |
print("π API endpoints:")
|
| 208 |
+
print(" - GET /")
|
| 209 |
print(" - GET /health")
|
| 210 |
+
print(" - POST /generate")
|
| 211 |
+
print(" - GET /api-test")
|
| 212 |
+
print(" - UI /ui (Gradio interface)")
|
| 213 |
print("=" * 60)
|
| 214 |
|
|
|
|
| 215 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|