Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,4 +1,76 @@
|
|
|
|
|
|
|
|
|
|
1 |
import os
|
|
|
|
|
2 |
|
|
|
|
|
3 |
|
4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from gradio_client import Client
|
3 |
+
import cloudinary
|
4 |
+
import cloudinary.uploader
|
5 |
import os
|
6 |
+
import requests
|
7 |
+
import json
|
8 |
|
9 |
+
# Cloudinary config
|
10 |
+
exec(os.environ.get('CLOUDINARY_CONFIG'))
|
11 |
|
12 |
+
|
13 |
+
# Initalize clip
|
14 |
+
clip = os.environ.get('CLIP')
|
15 |
+
|
16 |
+
def mistral(caption):
|
17 |
+
url = os.environ.get('MISTRAL_URL')
|
18 |
+
|
19 |
+
# Define the prompt
|
20 |
+
prompt = "given the following caption please provide some fashion styling advice: " + caption
|
21 |
+
|
22 |
+
# Mistral API call
|
23 |
+
payload = json.dumps({
|
24 |
+
"key": "aBNTB2KE0wqdbswyz6GfWvAeEo0WXgcLQHTwcAFBeJBfgyRvnlKdWsChx2w0",
|
25 |
+
"messages": [
|
26 |
+
{
|
27 |
+
"role": "user",
|
28 |
+
"content": prompt
|
29 |
+
},
|
30 |
+
],
|
31 |
+
"max_tokens": 1000
|
32 |
+
})
|
33 |
+
|
34 |
+
headers = {
|
35 |
+
'Content-Type': 'application/json'
|
36 |
+
}
|
37 |
+
|
38 |
+
# API response
|
39 |
+
response = requests.request("POST", url, headers=headers, data=payload)
|
40 |
+
response = json.loads(response.text)
|
41 |
+
|
42 |
+
return response['message']
|
43 |
+
|
44 |
+
def styling_advice(img):
|
45 |
+
# Upload image to cloudinary
|
46 |
+
uploaded_image = cloudinary.uploader.upload(img)
|
47 |
+
|
48 |
+
# Get image url
|
49 |
+
uploaded_image_url = uploaded_image['url']
|
50 |
+
|
51 |
+
# Get caption from image
|
52 |
+
caption = clip.predict(uploaded_image_url, "fast", 2, api_name="/clipi2")[0]
|
53 |
+
|
54 |
+
# Given the image caption, ask mistral for styling advice
|
55 |
+
advice = mistral(caption)
|
56 |
+
|
57 |
+
# Delete the image from cloudinary
|
58 |
+
cloudinary.uploader.destroy(public_id = uploaded_image['public_id'])
|
59 |
+
|
60 |
+
return advice
|
61 |
+
|
62 |
+
iface = gr.Interface(fn=styling_advice,
|
63 |
+
inputs=gr.Image(label="Your Photo", type='filepath'),
|
64 |
+
outputs=gr.Textbox(label='Fashion Stylist Bot'),
|
65 |
+
theme=gr.themes.Base(primary_hue=gr.themes.colors.pink, secondary_hue=gr.themes.colors.gray, neutral_hue=gr.themes.colors.teal, font=["avenir"]),
|
66 |
+
title="Fashion Stylist Bot",
|
67 |
+
description="""
|
68 |
+
by [Tony Assi](https://www.tonyassi.com/)
|
69 |
+
|
70 |
+
Please ❤️ this Space.
|
71 |
+
|
72 |
+
I build custom AI apps for companies. <a href="mailto: tony.assi.media@gmail.com">Email me</a> for business inquiries.
|
73 |
+
""",
|
74 |
+
examples=['./images/bella.jpg','./images/kendall.jpg','./images/paris.jpg']
|
75 |
+
)
|
76 |
+
iface.launch()
|