File size: 2,353 Bytes
99d8791
 
 
 
13996b8
99d8791
 
13996b8
99d8791
 
c2d66b9
99d8791
ccab127
99d8791
 
 
 
 
 
 
 
 
24c4acc
99d8791
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import gradio as gr
from gradio_client import Client
import cloudinary
import cloudinary.uploader
import os
import requests
import json

# Cloudinary config
exec(os.environ.get('CLOUDINARY_CONFIG'))

# Initalize clip
clip = Client(os.environ.get('CLIP'))

def mistral(caption):
    url = os.environ.get('MISTRAL_URL')
    
    # Define the prompt
    prompt = "given the following caption please provide some fashion styling advice: " + caption

    # Mistral API call
    payload = json.dumps({
      "key": os.environ.get('MISTRAL_KEY'),
      "messages": [
              {
                  "role": "user",
                  "content": prompt
              },
          ],
      "max_tokens": 1000
    })
    
    headers = {
      'Content-Type': 'application/json'
    }

    # API response
    response = requests.request("POST", url, headers=headers, data=payload)
    response = json.loads(response.text)

    return response['message']

def styling_advice(img):
    # Upload image to cloudinary
    uploaded_image = cloudinary.uploader.upload(img)

    # Get image url
    uploaded_image_url = uploaded_image['url']
    
    # Get caption from image
    caption = clip.predict(uploaded_image_url, "fast", 2, api_name="/clipi2")[0]

    # Given the image caption, ask mistral for styling advice
    advice = mistral(caption)

    # Delete the image from cloudinary
    cloudinary.uploader.destroy(public_id = uploaded_image['public_id'])
    
    return advice

iface = gr.Interface(fn=styling_advice, 
                     inputs=gr.Image(label="Your Photo", type='filepath'), 
                     outputs=gr.Textbox(label='Fashion Stylist Bot'), 
                     theme=gr.themes.Base(primary_hue=gr.themes.colors.pink, secondary_hue=gr.themes.colors.gray, neutral_hue=gr.themes.colors.teal, font=["avenir"]),
                     title="Fashion Stylist Bot",
                     description="""
                     by [Tony Assi](https://www.tonyassi.com/)
                     
                     Please ❤️ this Space.
        
                     I build custom AI apps for companies. <a href="mailto: tony.assi.media@gmail.com">Email me</a> for business inquiries.
                     """,
                     examples=['./images/bella.jpg','./images/kendall.jpg','./images/paris.jpg']
                     )
iface.launch()