pratikshahp commited on
Commit
891de68
Β·
verified Β·
1 Parent(s): da5f168

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -34
app.py CHANGED
@@ -1,4 +1,5 @@
1
  import gradio as gr
 
2
  import requests
3
  from PIL import Image
4
  import io
@@ -17,56 +18,56 @@ Compliment: You are the epitome of elegance and grace, with a style that is as t
17
  Conversation begins below:
18
  """
19
 
 
20
  def generate_compliment(image):
21
  # Convert PIL image to bytes
22
  buffered = io.BytesIO()
23
  image.save(buffered, format="JPEG")
24
  image_bytes = buffered.getvalue()
25
 
26
- # Connect to the captioning model on Hugging Face Spaces using the correct URL and method
27
- captioning_url = "https://gokaygokay-sd3-long-captioner.hf.space/run/create_captions_rich"
28
  try:
29
- caption_response = requests.post(captioning_url, files={"image": ("image.jpg", image_bytes, "image/jpeg")})
30
- caption_response.raise_for_status() # Raise an exception for HTTP errors
31
- except requests.exceptions.RequestException as e:
 
 
 
 
 
 
 
32
  return "Error", f"Failed to get caption. Exception: {e}"
33
 
34
  try:
35
- caption = caption_response.json()["data"][0]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36
  except Exception as e:
37
- return "Error", f"Failed to parse caption response. Error: {str(e)}, Response: {caption_response.text}"
38
-
39
- # Connect to the LLM model on Hugging Face Spaces
40
- llm_url = "https://hysts-zephyr-7b.hf.space/run/chat"
41
- llm_payload = {
42
- "system_prompt": SYSTEM_PROMPT,
43
- "message": f"Caption: {caption}\nCompliment: ",
44
- "max_new_tokens": 256,
45
- "temperature": 0.7,
46
- "top_p": 0.95,
47
- "top_k": 50,
48
- "repetition_penalty": 1,
49
- }
50
- try:
51
- llm_response = requests.post(llm_url, json=llm_payload)
52
- llm_response.raise_for_status() # Raise an exception for HTTP errors
53
- except requests.exceptions.RequestException as e:
54
- return "Error", f"Failed to get compliment. Exception: {e}"
55
 
56
- try:
57
- compliment = llm_response.json()["data"][0]
58
- except Exception as e:
59
- return "Error", f"Failed to parse LLM response. Error: {str(e)}, Response: {llm_response.text}"
60
-
61
- return caption, compliment
62
 
63
- # Gradio interface
64
  iface = gr.Interface(
65
  fn=generate_compliment,
66
- inputs=gr.Image(type="pil"),
67
  outputs=[
68
- gr.Textbox(label="Caption"),
69
- gr.Textbox(label="Compliment")
70
  ],
71
  title="Compliment Bot πŸ’–",
72
  description="Upload your headshot and get a personalized compliment!"
 
1
  import gradio as gr
2
+ import spaces
3
  import requests
4
  from PIL import Image
5
  import io
 
18
  Conversation begins below:
19
  """
20
 
21
+ # Function to generate compliment
22
  def generate_compliment(image):
23
  # Convert PIL image to bytes
24
  buffered = io.BytesIO()
25
  image.save(buffered, format="JPEG")
26
  image_bytes = buffered.getvalue()
27
 
 
 
28
  try:
29
+ # Connect to the captioning space
30
+ captioning_space = spaces.connect("gokaygokay/sd3-long-captioner")
31
+
32
+ # Predict caption for the provided image
33
+ caption = captioning_space.predict("/create_captions_rich", { "image": image_bytes })
34
+
35
+ # Extract the caption from the response
36
+ caption_text = caption.data[0]
37
+
38
+ except Exception as e:
39
  return "Error", f"Failed to get caption. Exception: {e}"
40
 
41
  try:
42
+ # Connect to the LLM space
43
+ llm_space = spaces.connect("hysts/zephyr-7b")
44
+
45
+ # Generate compliment using the caption
46
+ llm_payload = {
47
+ "system_prompt": SYSTEM_PROMPT,
48
+ "message": f"Caption: {caption_text}\nCompliment: ",
49
+ "max_new_tokens": 256,
50
+ "temperature": 0.7,
51
+ "top_p": 0.95,
52
+ "top_k": 50,
53
+ "repetition_penalty": 1,
54
+ }
55
+
56
+ compliment = llm_space.run(llm_payload)
57
+ compliment_text = compliment.data[0]
58
+
59
  except Exception as e:
60
+ return "Error", f"Failed to generate compliment. Exception: {e}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
61
 
62
+ return caption_text, compliment_text
 
 
 
 
 
63
 
64
+ # Gradio interface setup
65
  iface = gr.Interface(
66
  fn=generate_compliment,
67
+ inputs=gr.inputs.Image(type="pil", label="Upload Image"),
68
  outputs=[
69
+ gr.outputs.Textbox(label="Caption"),
70
+ gr.outputs.Textbox(label="Compliment")
71
  ],
72
  title="Compliment Bot πŸ’–",
73
  description="Upload your headshot and get a personalized compliment!"