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. Email me for business inquiries. """, examples=['./images/bella.jpg','./images/kendall.jpg','./images/paris.jpg'] ) iface.launch()