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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +7 -21
app.py CHANGED
@@ -1,25 +1,11 @@
1
  from gradio_client import Client, file
2
  import gradio as gr
3
  from PIL import Image
4
- import requests
5
  import io
6
 
7
  # Configuration for Hugging Face Spaces
8
  CAPTION_SPACE = "gokaygokay/SD3-Long-Captioner"
9
  LLM_SPACE = "hysts/zephyr-7b"
10
- SYSTEM_PROMPT = """
11
- You are a helpful assistant that gives the best compliments to people.
12
- You will be given a caption of someone's headshot.
13
- Based on that caption, provide a one sentence compliment to the person in the image.
14
- Make sure you compliment the person in the image and not any objects or scenery.
15
- Do NOT include any hashtags in your compliment or phrases like (emojis: dog, smiling face with heart-eyes, sun).
16
- Here are some examples of the desired behavior:
17
- Caption: a front view of a man who is smiling, there is a lighthouse in the background, there is a grassy area on the left that is green and curved. in the distance you can see the ocean and the shore. there is a grey and cloudy sky above the lighthouse and the trees.
18
- Compliment: Your smile is as bright as a lighthouse, lighting up the world around you. 🌟
19
- Caption: in a close-up, a blonde woman with short, wavy hair, is the focal point of the image. she's dressed in a dark brown turtleneck sweater, paired with a black hat and a black suit jacket. her lips are a vibrant red, and her eyes are a deep brown. in the background, a man with a black hat and a white shirt is visible.
20
- Compliment: You are the epitome of elegance and grace, with a style that is as timeless as your beauty. πŸ’ƒπŸŽ©
21
- Conversation begins below:
22
- """
23
 
24
  # Initialize Gradio client for captioning and language model
25
  captioning_client = Client(CAPTION_SPACE)
@@ -34,18 +20,16 @@ def generate_compliment(image):
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)}"
@@ -61,5 +45,7 @@ iface = gr.Interface(
61
  gr.Textbox(label="Compliment")
62
  ],
63
  title="Compliment Bot πŸ’–",
64
- description="Upload your headshot and get a personalized compliment!"
 
65
  )
 
 
1
  from gradio_client import Client, file
2
  import gradio as gr
3
  from PIL import Image
 
4
  import io
5
 
6
  # Configuration for Hugging Face Spaces
7
  CAPTION_SPACE = "gokaygokay/SD3-Long-Captioner"
8
  LLM_SPACE = "hysts/zephyr-7b"
 
 
 
 
 
 
 
 
 
 
 
 
 
9
 
10
  # Initialize Gradio client for captioning and language model
11
  captioning_client = Client(CAPTION_SPACE)
 
20
  image.save(buffered, format="JPEG")
21
  image_bytes = buffered.getvalue()
22
 
23
+ # Retrieve caption from the captioning model
24
  try:
25
+ caption_response = captioning_client.predict("/create_captions_rich", {"image": file(image_bytes)})
 
26
  caption_text = caption_response.data[0]
27
  except Exception as e:
28
  return "Error", f"Failed to get caption. Exception: {str(e)}"
29
 
30
+ # Generate compliment using the language model
31
  try:
32
+ llm_response = llm_client.predict({"system_prompt": SYSTEM_PROMPT, "message": f"Caption: {caption_text}\nCompliment: "})
 
33
  compliment_text = llm_response.data[0]
34
  except Exception as e:
35
  return "Error", f"Failed to generate compliment. Exception: {str(e)}"
 
45
  gr.Textbox(label="Compliment")
46
  ],
47
  title="Compliment Bot πŸ’–",
48
+ description="Upload your headshot and get a personalized compliment!",
49
+ live=True # Set live=True to launch the interface immediately
50
  )
51
+ iface.launch()