Dash-inc commited on
Commit
d95a2ab
·
verified ·
1 Parent(s): f151981

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +19 -4
main.py CHANGED
@@ -99,7 +99,7 @@ async def new_chat():
99
  return {"chat_id": chat_id}
100
 
101
  @app.post("/generate-image", response_model=dict)
102
- async def generate_image(request: ImageRequest, background_tasks: BackgroundTasks):
103
  def process_request():
104
  chat_history = get_chat_history(request.chat_id)
105
  prompt = f"""
@@ -145,9 +145,24 @@ async def generate_image(request: ImageRequest, background_tasks: BackgroundTask
145
  filepath = save_image_locally(img, filename)
146
  return filepath
147
 
148
- # Add the wrapper function to background tasks
149
- background_tasks.add_task(lambda: executor.submit(process_request))
150
- return {"status": "Processing"}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
151
 
152
  @app.post("/upscale-image", response_model=dict)
153
  async def upscale_image(image_url: str, background_tasks: BackgroundTasks):
 
99
  return {"chat_id": chat_id}
100
 
101
  @app.post("/generate-image", response_model=dict)
102
+ async def generate_image(request: ImageRequest):
103
  def process_request():
104
  chat_history = get_chat_history(request.chat_id)
105
  prompt = f"""
 
145
  filepath = save_image_locally(img, filename)
146
  return filepath
147
 
148
+ # Run the request processing in a thread
149
+ future = executor.submit(process_request)
150
+ filepath = await run_in_threadpool(future.result) # Wait for the task to complete
151
+
152
+ # Load the image to return as a response
153
+ with open(filepath, "rb") as f:
154
+ image_data = f.read()
155
+
156
+ # Convert the file path into a downloadable format
157
+ file_url = f"/images/{os.path.basename(filepath)}"
158
+
159
+ # Return the response
160
+ return {
161
+ "status": "Image generated successfully",
162
+ "file_path": filepath,
163
+ "file_url": file_url,
164
+ "image": image_data,
165
+ }
166
 
167
  @app.post("/upscale-image", response_model=dict)
168
  async def upscale_image(image_url: str, background_tasks: BackgroundTasks):