Dash-inc commited on
Commit
873d584
·
verified ·
1 Parent(s): 6a44c93

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +14 -5
main.py CHANGED
@@ -33,7 +33,6 @@ app.add_middleware(
33
  # Globals
34
  executor = ThreadPoolExecutor(max_workers=10)
35
  llm = None
36
- aura_sr = None
37
  client = MongoClient(f"mongodb+srv://hammad:{quote_plus('momimaad@123')}@cluster0.2a9yu.mongodb.net/")
38
  db = client["Flux"]
39
  collection = db["chat_histories"]
@@ -51,14 +50,20 @@ image_store = {}
51
 
52
  @app.on_event("startup")
53
  async def startup():
54
- global llm, upscale_model
55
  llm = ChatGroq(
56
  model="llama-3.3-70b-versatile",
57
  temperature=0.7,
58
  max_tokens=1024,
59
  api_key="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",
60
  )
61
- aura_sr = AuraSR.from_pretrained("fal/AuraSR-v2")
 
 
 
 
 
 
62
 
63
  @app.on_event("shutdown")
64
  def shutdown():
@@ -196,19 +201,23 @@ class UpscaleRequest(BaseModel):
196
 
197
 
198
  def process_image(image_url):
 
 
 
199
  response = requests.get(image_url)
200
  img = Image.open(BytesIO(response.content))
201
- if aura_sr is None:
202
- raise RuntimeError("aura_sr is None. Check initialization.")
203
  try:
204
  upscaled_image = aura_sr.upscale_4x_overlapped(img)
205
  except Exception as e:
206
  raise RuntimeError(f"Error during upscaling: {str(e)}")
 
207
  filename = f"upscaled_{uuid.uuid4()}.png"
208
  filepath = save_image_locally(upscaled_image, filename)
209
  return filepath
210
 
211
 
 
212
  @app.post("/upscale-image", response_model=dict)
213
  async def upscale_image(request: UpscaleRequest, background_tasks: BackgroundTasks):
214
  # Add the task directly to the background
 
33
  # Globals
34
  executor = ThreadPoolExecutor(max_workers=10)
35
  llm = None
 
36
  client = MongoClient(f"mongodb+srv://hammad:{quote_plus('momimaad@123')}@cluster0.2a9yu.mongodb.net/")
37
  db = client["Flux"]
38
  collection = db["chat_histories"]
 
50
 
51
  @app.on_event("startup")
52
  async def startup():
53
+ global llm, aura_sr
54
  llm = ChatGroq(
55
  model="llama-3.3-70b-versatile",
56
  temperature=0.7,
57
  max_tokens=1024,
58
  api_key="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",
59
  )
60
+ # Ensure AuraSR is initialized properly
61
+ try:
62
+ aura_sr = AuraSR.from_pretrained("fal/AuraSR-v2")
63
+ except Exception as e:
64
+ print(f"Error initializing AuraSR: {e}")
65
+ aura_sr = None
66
+
67
 
68
  @app.on_event("shutdown")
69
  def shutdown():
 
201
 
202
 
203
  def process_image(image_url):
204
+ if aura_sr is None:
205
+ raise RuntimeError("aura_sr is None. Ensure it's initialized during startup.")
206
+
207
  response = requests.get(image_url)
208
  img = Image.open(BytesIO(response.content))
209
+
 
210
  try:
211
  upscaled_image = aura_sr.upscale_4x_overlapped(img)
212
  except Exception as e:
213
  raise RuntimeError(f"Error during upscaling: {str(e)}")
214
+
215
  filename = f"upscaled_{uuid.uuid4()}.png"
216
  filepath = save_image_locally(upscaled_image, filename)
217
  return filepath
218
 
219
 
220
+
221
  @app.post("/upscale-image", response_model=dict)
222
  async def upscale_image(request: UpscaleRequest, background_tasks: BackgroundTasks):
223
  # Add the task directly to the background