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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +57 -33
app.py CHANGED
@@ -35,7 +35,7 @@ BACKUP_DIR = "generated_images_backup"
35
  os.makedirs(BACKUP_DIR, exist_ok=True)
36
 
37
  # Initialize FastAPI
38
- app = FastAPI()
39
 
40
  # Global model cache
41
  model = None
@@ -130,13 +130,13 @@ def generate_image(prompt, project_id=None, scene_num=1):
130
  raise
131
 
132
  # =============================================
133
- # API ENDPOINTS (Define these BEFORE mounting Gradio)
134
  # =============================================
135
  class GenerateRequest(BaseModel):
136
  prompt: str
137
  project_id: Optional[str] = None
138
 
139
- @app.post("/generate")
140
  async def generate(request: GenerateRequest):
141
  """Generate a single image"""
142
  try:
@@ -149,7 +149,7 @@ async def generate(request: GenerateRequest):
149
  except Exception as e:
150
  return {"status": "error", "message": str(e)}
151
 
152
- @app.get("/health")
153
  async def health():
154
  """Health check"""
155
  return {
@@ -158,13 +158,13 @@ async def health():
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:
@@ -172,44 +172,68 @@ def gradio_generate(prompt):
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)
 
35
  os.makedirs(BACKUP_DIR, exist_ok=True)
36
 
37
  # Initialize FastAPI
38
+ app = FastAPI(title="Image Generator API")
39
 
40
  # Global model cache
41
  model = None
 
130
  raise
131
 
132
  # =============================================
133
+ # API ENDPOINTS
134
  # =============================================
135
  class GenerateRequest(BaseModel):
136
  prompt: str
137
  project_id: Optional[str] = None
138
 
139
+ @app.post("/api/generate") # Changed to /api/generate
140
  async def generate(request: GenerateRequest):
141
  """Generate a single image"""
142
  try:
 
149
  except Exception as e:
150
  return {"status": "error", "message": str(e)}
151
 
152
+ @app.get("/api/health") # Changed to /api/health
153
  async def health():
154
  """Health check"""
155
  return {
 
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"""
164
+ return {"message": "API is working!"}
165
 
166
  # =============================================
167
+ # GRADIO INTERFACE
168
  # =============================================
169
  def gradio_generate(prompt):
170
  if not prompt:
 
172
  result = generate_image(prompt)
173
  return result["image"]
174
 
175
+ # Create Gradio interface
176
+ with gr.Blocks(title="Image Generator", theme=gr.themes.Soft()) as demo:
177
+ gr.Markdown("# 🎨 Image Generator")
178
+ gr.Markdown("Generate images using Stable Diffusion")
179
+
180
+ with gr.Row():
181
+ with gr.Column():
182
+ prompt_input = gr.Textbox(
183
+ label="Prompt",
184
+ placeholder="Enter your prompt...",
185
+ lines=3
186
+ )
187
+ generate_btn = gr.Button("Generate", variant="primary")
188
+
189
+ with gr.Column():
190
+ output_image = gr.Image(label="Generated Image")
191
+
192
+ generate_btn.click(
193
+ fn=gradio_generate,
194
+ inputs=[prompt_input],
195
+ outputs=[output_image]
196
+ )
197
+
198
+ gr.Markdown("---")
199
+ gr.Markdown("### API Endpoints")
200
+ gr.Markdown("""
201
+ - `GET /api/health` - Health check
202
+ - `POST /api/generate` - Generate image
203
+ - `GET /api/test` - Test endpoint
204
+ """)
205
 
206
+ # =============================================
207
+ # ROOT ENDPOINT
208
+ # =============================================
209
  @app.get("/")
210
  async def root():
211
  return {
212
+ "name": "Image Generator",
213
+ "version": "1.0.0",
214
+ "api_endpoints": {
215
+ "health": "GET /api/health",
216
+ "generate": "POST /api/generate",
217
+ "test": "GET /api/test"
218
  },
219
+ "ui": "Gradio interface available at /ui",
220
  "status": "running"
221
  }
222
 
223
  # =============================================
224
+ # MAIN - Hugging Face Spaces Deployment
225
  # =============================================
226
  if __name__ == "__main__":
227
  import uvicorn
228
+
229
  print("\n" + "=" * 60)
230
+ print("🌐 Deploying on Hugging Face Spaces")
231
+ print("πŸ“š API endpoints: /api/*")
232
+ print("🎨 UI: /ui")
 
 
 
 
233
  print("=" * 60)
234
 
235
+ # Mount Gradio at /ui path
236
+ app = gr.mount_gradio_app(app, demo, path="/ui")
237
+
238
+ # Run the combined app
239
  uvicorn.run(app, host="0.0.0.0", port=7860)