Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,5 +1,4 @@
|
|
1 |
import gradio as gr
|
2 |
-
import spaces
|
3 |
import requests
|
4 |
from PIL import Image
|
5 |
import io
|
@@ -18,48 +17,49 @@ Compliment: You are the epitome of elegance and grace, with a style that is as t
|
|
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 |
-
|
30 |
-
|
31 |
-
|
32 |
-
# Predict caption for the provided image
|
33 |
-
caption_response = captioning_space.predict("/create_captions_rich", { "image": image_bytes })
|
34 |
-
caption_text = caption_response.data[0]
|
35 |
-
|
36 |
-
except Exception as e:
|
37 |
return "Error", f"Failed to get caption. Exception: {e}"
|
38 |
|
39 |
try:
|
40 |
-
|
41 |
-
llm_space = spaces.connect("hysts/zephyr-7b")
|
42 |
-
|
43 |
-
# Generate compliment using the caption
|
44 |
-
llm_payload = {
|
45 |
-
"system_prompt": SYSTEM_PROMPT,
|
46 |
-
"message": f"Caption: {caption_text}\nCompliment: ",
|
47 |
-
"max_new_tokens": 256,
|
48 |
-
"temperature": 0.7,
|
49 |
-
"top_p": 0.95,
|
50 |
-
"top_k": 50,
|
51 |
-
"repetition_penalty": 1,
|
52 |
-
}
|
53 |
-
|
54 |
-
compliment_response = llm_space.run(llm_payload)
|
55 |
-
compliment_text = compliment_response.data[0]
|
56 |
-
|
57 |
except Exception as e:
|
58 |
-
return "Error", f"Failed to
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
59 |
|
60 |
-
|
|
|
|
|
|
|
61 |
|
62 |
-
|
|
|
63 |
# Gradio interface
|
64 |
iface = gr.Interface(
|
65 |
fn=generate_compliment,
|
|
|
1 |
import gradio as gr
|
|
|
2 |
import requests
|
3 |
from PIL import Image
|
4 |
import io
|
|
|
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
|
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,
|