Fraser commited on
Commit
9125925
·
1 Parent(s): 716bbdf
Files changed (2) hide show
  1. .vscode/settings.json +3 -0
  2. app.py +13 -5
.vscode/settings.json ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ {
2
+ "python-envs.pythonProjects": []
3
+ }
app.py CHANGED
@@ -256,8 +256,13 @@ class PicletGeneratorService:
256
  FLUX_SPACE = "black-forest-labs/FLUX.1-schnell"
257
 
258
  @staticmethod
259
- async def generate_enhanced_caption(image_path: str, hf_token: str) -> str:
260
- """Generate detailed image description using JoyCaption"""
 
 
 
 
 
261
  try:
262
  print(f"Connecting to JoyCaption space with user token...")
263
  client = Client(
@@ -267,7 +272,7 @@ class PicletGeneratorService:
267
 
268
  print(f"Generating caption for image...")
269
  result = await client.predict(
270
- image=image_path,
271
  caption_type="Descriptive",
272
  caption_length="medium-length",
273
  extra_options=[],
@@ -808,12 +813,15 @@ async def generate_piclet(image, hf_token: str) -> dict:
808
  # Get user profile (creates if doesn't exist)
809
  user_profile = PicletDiscoveryService.get_or_create_user_profile(user_info)
810
 
811
- # Extract image path from Gradio file input
 
 
812
  image_path = image if isinstance(image, str) else image.name if hasattr(image, 'name') else str(image)
 
813
 
814
  # Step 1: Generate caption
815
  print("Step 1/5: Generating image caption...")
816
- caption = await PicletGeneratorService.generate_enhanced_caption(image_path, hf_token)
817
 
818
  # Step 2: Generate concept
819
  print("Step 2/5: Generating Piclet concept...")
 
256
  FLUX_SPACE = "black-forest-labs/FLUX.1-schnell"
257
 
258
  @staticmethod
259
+ async def generate_enhanced_caption(image_data, hf_token: str) -> str:
260
+ """Generate detailed image description using JoyCaption
261
+
262
+ Args:
263
+ image_data: PIL Image or file path
264
+ hf_token: User's HuggingFace token
265
+ """
266
  try:
267
  print(f"Connecting to JoyCaption space with user token...")
268
  client = Client(
 
272
 
273
  print(f"Generating caption for image...")
274
  result = await client.predict(
275
+ image=image_data,
276
  caption_type="Descriptive",
277
  caption_length="medium-length",
278
  extra_options=[],
 
813
  # Get user profile (creates if doesn't exist)
814
  user_profile = PicletDiscoveryService.get_or_create_user_profile(user_info)
815
 
816
+ # Extract image from Gradio input and read it immediately to avoid cleanup issues
817
+ # Gradio cleans up temp files, so we read the image data right away
818
+ from PIL import Image as PILImage
819
  image_path = image if isinstance(image, str) else image.name if hasattr(image, 'name') else str(image)
820
+ image_data = PILImage.open(image_path)
821
 
822
  # Step 1: Generate caption
823
  print("Step 1/5: Generating image caption...")
824
+ caption = await PicletGeneratorService.generate_enhanced_caption(image_data, hf_token)
825
 
826
  # Step 2: Generate concept
827
  print("Step 2/5: Generating Piclet concept...")