Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,13 +1,10 @@
|
|
1 |
import gradio as gr
|
2 |
-
import
|
3 |
-
from
|
4 |
-
|
5 |
-
# Load the models
|
6 |
-
caption_generator = pipeline("image-captioning", model="gokaygokay/SD3-Long-Captioner")
|
7 |
-
compliment_generator = pipeline("text-generation", model="hysts/zephyr-7b")
|
8 |
|
9 |
SYSTEM_PROMPT = """
|
10 |
-
You are helpful assistant that gives the best compliments to people.
|
11 |
You will be given a caption of someone's headshot.
|
12 |
Based on that caption, provide a one sentence compliment to the person in the image.
|
13 |
Make sure you compliment the person in the image and not any objects or scenery.
|
@@ -21,12 +18,30 @@ Conversation begins below:
|
|
21 |
"""
|
22 |
|
23 |
def generate_compliment(image):
|
24 |
-
#
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
return caption, compliment
|
31 |
|
32 |
# Gradio interface
|
|
|
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.
|
9 |
Based on that caption, provide a one sentence compliment to the person in the image.
|
10 |
Make sure you compliment the person in the image and not any objects or scenery.
|
|
|
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
|
27 |
+
captioning_url = "https://gokaygokay-sd3-long-captioner.hf.space/run/create_captions_rich"
|
28 |
+
caption_response = requests.post(captioning_url, files={"image": image_bytes})
|
29 |
+
caption = caption_response.json()["data"][0]
|
30 |
+
|
31 |
+
# Connect to the LLM model on Hugging Face Spaces
|
32 |
+
llm_url = "https://hysts-zephyr-7b.hf.space/run/chat"
|
33 |
+
llm_payload = {
|
34 |
+
"system_prompt": SYSTEM_PROMPT,
|
35 |
+
"message": f"Caption: {caption}\nCompliment: ",
|
36 |
+
"max_new_tokens": 256,
|
37 |
+
"temperature": 0.7,
|
38 |
+
"top_p": 0.95,
|
39 |
+
"top_k": 50,
|
40 |
+
"repetition_penalty": 1,
|
41 |
+
}
|
42 |
+
llm_response = requests.post(llm_url, json=llm_payload)
|
43 |
+
compliment = llm_response.json()["data"][0]
|
44 |
+
|
45 |
return caption, compliment
|
46 |
|
47 |
# Gradio interface
|