yukee1992 commited on
Commit
8a0facd
Β·
verified Β·
1 Parent(s): 8da2480

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -27
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 (Optional UI)
163
  # =============================================
164
- if 'gradio' in globals():
165
- def gradio_generate(prompt):
166
- if not prompt:
167
- return None
168
- result = generate_image(prompt)
169
- return result["image"]
170
-
171
- iface = gr.Interface(
172
- fn=gradio_generate,
173
- inputs=gr.Textbox(label="Prompt", placeholder="Enter your prompt..."),
174
- outputs=gr.Image(label="Generated Image"),
175
- title="Image Generator",
176
- description="Generate images with Stable Diffusion"
177
- )
178
-
179
- # Mount Gradio
180
- gr.mount_gradio_app(app, iface, path="/")
181
- else:
182
- @app.get("/")
183
- async def root():
184
- return {"message": "API is running. Use /generate endpoint"}
 
 
 
 
 
 
 
 
 
185
 
186
  # =============================================
187
- # MAIN - CORRECTED VERSION
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(" - POST /generate")
195
  print(" - GET /health")
196
- print(" - GET / (root)")
 
 
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)