Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,8 +1,12 @@
|
|
|
|
1 |
import gradio as gr
|
2 |
-
import requests
|
3 |
from PIL import Image
|
|
|
4 |
import io
|
5 |
|
|
|
|
|
|
|
6 |
SYSTEM_PROMPT = """
|
7 |
You are a helpful assistant that gives the best compliments to people.
|
8 |
You will be given a caption of someone's headshot.
|
@@ -17,53 +21,35 @@ 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 |
-
|
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
|
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 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
58 |
except Exception as e:
|
59 |
-
|
|
|
|
|
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")
|
|
|
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.
|
|
|
21 |
Conversation begins below:
|
22 |
"""
|
23 |
|
24 |
+
# Initialize Gradio client for captioning and language model
|
25 |
+
captioning_client = Client(CAPTION_SPACE)
|
26 |
+
llm_client = Client(LLM_SPACE)
|
27 |
+
|
28 |
def generate_compliment(image):
|
29 |
+
compliment_text = ""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
try:
|
31 |
+
# Convert PIL image to bytes
|
32 |
+
buffered = io.BytesIO()
|
33 |
+
image.save(buffered, format="JPEG")
|
34 |
+
image_bytes = buffered.getvalue()
|
35 |
+
|
36 |
+
# Get caption for the image using Gradio client
|
37 |
+
caption_response = captioning_client.predict("/create_captions_rich", { "image": file(image_bytes) })
|
38 |
+
caption_text = caption_response.data[0]
|
39 |
+
|
40 |
+
# Generate compliment based on the caption using language model
|
41 |
+
llm_response = llm_client.predict(SYSTEM_PROMPT, f"Caption: {caption_text}\nCompliment: ")
|
42 |
+
compliment_text = llm_response.data[0]
|
43 |
+
|
44 |
except Exception as e:
|
45 |
+
compliment_text = f"Error: {str(e)}"
|
46 |
+
|
47 |
+
return caption_text, compliment_text
|
48 |
|
|
|
|
|
49 |
# Gradio interface
|
50 |
iface = gr.Interface(
|
51 |
fn=generate_compliment,
|
52 |
+
inputs=gr.Image(type="pil"),
|
53 |
outputs=[
|
54 |
gr.Textbox(label="Caption"),
|
55 |
gr.Textbox(label="Compliment")
|