pratikshahp commited on
Commit
12c8ac7
·
verified ·
1 Parent(s): 2237481

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -14
app.py CHANGED
@@ -26,24 +26,30 @@ captioning_client = Client(CAPTION_SPACE)
26
  llm_client = Client(LLM_SPACE)
27
 
28
  def generate_compliment(image):
 
 
 
 
 
 
 
 
 
29
  try:
30
- # Convert PIL image to bytes
31
- buffered = io.BytesIO()
32
- image.save(buffered, format="JPEG")
33
- image_bytes = buffered.getvalue()
34
-
35
- # Get caption for the image using Gradio client
36
- caption_response = captioning_client.predict("/create_captions_rich", { "image": file(image_bytes) })
37
  caption_text = caption_response.data[0]
38
-
39
- # Generate compliment based on the caption using language model
40
- llm_response = llm_client.predict(SYSTEM_PROMPT, f"Caption: {caption_text}\nCompliment: ")
 
 
 
 
41
  compliment_text = llm_response.data[0]
42
-
43
  except Exception as e:
44
- caption_text = f"Error: Failed to get caption. Exception: {str(e)}"
45
- compliment_text = "Error: Failed to generate compliment."
46
-
47
  return caption_text, compliment_text
48
 
49
  # Gradio interface
 
26
  llm_client = Client(LLM_SPACE)
27
 
28
  def generate_compliment(image):
29
+ caption_text = ""
30
+ compliment_text = ""
31
+
32
+ # Convert PIL image to bytes
33
+ buffered = io.BytesIO()
34
+ image.save(buffered, format="JPEG")
35
+ image_bytes = buffered.getvalue()
36
+
37
+ # Connect to the captioning model on Hugging Face Spaces
38
  try:
39
+ captioning_space = Client("gokaygokay/SD3-Long-Captioner")
40
+ caption_response = captioning_space.predict("/create_captions_rich", {"image": file(image_bytes)})
 
 
 
 
 
41
  caption_text = caption_response.data[0]
42
+ except Exception as e:
43
+ return "Error", f"Failed to get caption. Exception: {str(e)}"
44
+
45
+ # Connect to the LLM model on Hugging Face Spaces
46
+ try:
47
+ llm_space = Client("hysts/zephyr-7b")
48
+ llm_response = llm_space.predict({"system_prompt": "...", "message": f"Caption: {caption_text}\nCompliment: "})
49
  compliment_text = llm_response.data[0]
 
50
  except Exception as e:
51
+ return "Error", f"Failed to generate compliment. Exception: {str(e)}"
52
+
 
53
  return caption_text, compliment_text
54
 
55
  # Gradio interface