ajitesh123 / AI-Stylist

OpenAI GPT-4 With Vision 🔥 This repository features an outfit rating app using OpenAI's GPT-4 with the Vision extension. Users can upload images through a Streamlit interface, and the app provides both a fashion stylist's and a mother's verdict on the outfit. Contributions are welcome!
MIT License
1 stars 0 forks source link

Sweep: Add an additonal feature in app.py to act as fashion stylist #6

Open ajitesh123 opened 2 weeks ago

ajitesh123 commented 2 weeks ago

Add an additional feature in app.py for LLM to act as fashion stylist and make deicion on whether to keep or return.

Also change from gradio to streamlit

sweep-ai-self-hosted-aj[bot] commented 2 weeks ago
Sweeping

75%

Actions

README.md

Update the README file to reflect the change from Gradio to Streamlit and mention the new fashion stylist feature.
--- 
+++ 
@@ -3,8 +3,7 @@
 emoji: 🔥
 colorFrom: yellow
 colorTo: indigo
-sdk: gradio
-sdk_version: 4.1.1
+sdk: streamlit
 app_file: app.py
 pinned: false
 license: mit
@@ -12,4 +11,4 @@

 # OpenAI GPT-4 With Vision

-This repository contains a simple image captioning app that utilizes OpenAI's GPT-4 with the Vision extension. Users can upload images through a Gradio interface, and the app leverages GPT-4 to generate a description of the image content.
+This repository contains an image captioning and fashion advice app that utilizes OpenAI's GPT-4 with the Vision extension. Users can upload images through a Streamlit interface, and the app leverages GPT-4 to generate a description of the image content. Additionally, users can upload clothing images and receive fashion advice on whether to keep or return the item, along with a justification.

app.py

Update the imports in app.py to remove Gradio and add Streamlit.
--- 
+++ 
@@ -1,5 +1,5 @@
 # Import the necessary libraries
-import gradio as gr
+import streamlit as st
 import openai
 import base64
 from PIL import Image

app.py

Refactor the `ask_openai_with_image` function to handle file objects instead of PIL images.
--- 
+++ 
@@ -1,8 +1,9 @@
 # Function to send the image to the OpenAI API and get a response
-def ask_openai_with_image(image):
-    # Encode the uploaded image to base64
+def ask_openai_with_image(image_file):
+    # Read the image file and encode it to base64
+    image = Image.open(image_file)
     base64_image = encode_image_to_base64(image)
-    
+
     # Create the payload with the base64 encoded image
     payload = {
         "model": "gpt-4-vision-preview",
@@ -23,14 +24,14 @@
         ],
         "max_tokens": 4095
     }
-    
+
     # Send the request to the OpenAI API
     response = requests.post(
         "https://api.openai.com/v1/chat/completions",
         headers={"Authorization": f"Bearer {openai.api_key}"},
         json=payload
     )
-    
+
     # Check if the request was successful
     if response.status_code == 200:
         response_json = response.json()

app.py

Rewritten instructions to resolve the error. Update the original_code and new_code blocks as required, ensuring that the
--- 
+++ 
@@ -10,7 +10,7 @@
     # Read the image file and encode it to base64
     image = Image.open(image_file)
     base64_image = encode_image_to_base64(image)
-    
+
     # Create the payload with the base64 encoded image
     payload = {
         "model": "gpt-4-vision-preview",
@@ -31,14 +31,14 @@
         ],
         "max_tokens": 4095
     }
-    
+
     # Send the request to the OpenAI API
     response = requests.post(
         "https://api.openai.com/v1/chat/completions",
         headers={"Authorization": f"Bearer {openai.api_key}"},
         json=payload
     )
-    
+
     # Check if the request was successful
     if response.status_code == 200:
         response_json = response.json()
@@ -53,4 +53,57 @@
             return "Error processing the image response."
     else:
         # If an error occurred, return the error message
+        return f"Error: {response.text}"
+
+# Function to send the image to the OpenAI API and get fashion advice
+def ask_openai_fashion_advice(image_file):
+    # Read the image file and encode it to base64
+    image = Image.open(image_file)
+    base64_image = encode_image_to_base64(image)
+
+    # Create the payload with the base64 encoded image
+    payload = {
+        "model": "gpt-4-vision-preview",
+        "messages": [
+            {
+                "role": "user",
+                "content": [
+                    {
+                        "type": "text",
+                        "text": "I've uploaded an image of a clothing item. Please provide fashion advice on whether I should keep or return this item, along with a justification for your decision."
+                    },
+                    {
+                        "type": "image_url",
+                        "image_url": f"data:image/jpeg;base64,{base64_image}"
+                    }
+                ]
+            }
+        ],
+        "max_tokens": 4095
+    }
+
+    # Send the request to the OpenAI API
+    response = requests.post(
+        "https://api.openai.com/v1/chat/completions",
+        headers={"Authorization": f"Bearer {openai.api_key}"},
+        json=payload
+    )
+
+    # Check if the request was successful
+    if response.status_code == 200:
+        response_json = response.json()
+        print("Response JSON:", response_json)  # Print the raw response JSON
+        try:
+            # Attempt to extract the content text
+            content = response_json["choices"][0]["message"]["content"]
+            # Split the content into decision and justification
+            decision, justification = content.split("\n", 1)
+            return decision.strip(), justification.strip()
+        except Exception as e:
+            # If there is an error in the JSON structure, print it
+            print("Error in JSON structure:", e)
+            print("Full JSON response:", response_json)
+            return "Error processing the fashion advice response."
+    else:
+        # If an error occurred, return the error message
         return f"Error: {response.text}"

app.py

Replace the Gradio interface with Streamlit components for the image captioning feature.
--- 
+++ 
@@ -1,11 +1,17 @@
-# Create a Gradio interface
-iface = gr.Interface(
-    fn=ask_openai_with_image,
-    inputs=gr.Image(type="pil"),
-    outputs="text",
-    title="GPT-4 with Vision",
-    description="Upload an image and get a description from GPT-4 with Vision."
-)
+# Streamlit app for image captioning
+st.title("GPT-4 with Vision")
+st.write("Upload an image and get a description from GPT-4 with Vision.")

-# Launch the app
-iface.launch()
+# File uploader for image captioning
+image_file = st.file_uploader("Choose an image", type=["jpg", "jpeg", "png"])
+
+if image_file is not None:
+    # Display the uploaded image
+    st.image(image_file, caption="Uploaded Image", use_column_width=True)
+
+    # Button to trigger image captioning
+    if st.button("Get Image Description"):
+        # Call the function to get image description
+        image_description = ask_openai_with_image(image_file)
+        st.write("Image Description:")
+        st.write(image_description)

app.py

Rewritten instructions to resolve the error. Update the original_code and new_code blocks as required, ensuring that the
--- 
+++ 
@@ -8,10 +8,29 @@
 if image_file is not None:
     # Display the uploaded image
     st.image(image_file, caption="Uploaded Image", use_column_width=True)
-    
+
     # Button to trigger image captioning
     if st.button("Get Image Description"):
         # Call the function to get image description
         image_description = ask_openai_with_image(image_file)
         st.write("Image Description:")
-        st.write(image_description)
+        st.write(image_description)
+
+# Streamlit app for fashion advice
+st.header("Fashion Stylist")
+st.write("Upload an image of a clothing item and get fashion advice from GPT-4.")
+
+# File uploader for fashion advice
+fashion_image_file = st.file_uploader("Choose a clothing image", type=["jpg", "jpeg", "png"])
+
+if fashion_image_file is not None:
+    # Display the uploaded image
+    st.image(fashion_image_file, caption="Uploaded Clothing Image", use_column_width=True)
+
+    # Button to trigger fashion advice
+    if st.button("Get Fashion Advice"):
+        # Call the function to get fashion advice
+        decision, justification = ask_openai_fashion_advice(fashion_image_file)
+        st.write("Fashion Advice:")
+        st.write(f"Decision: {decision}")
+        st.write(f"Justification: {justification}")

app.py

Update the app launching code to run the Streamlit app.
--- 
+++ 
@@ -1,2 +1,2 @@
-# Launch the app
-iface.launch()
+if __name__ == "__main__":
+    st.run()

Feel free to add more details to the issue description so Sweep can better address it. Alternatively, reach out to Kevin or William for help at https://community.sweep.dev/.

Report a bug.


[!TIP] To recreate the pull request, edit the issue title or description.

This is an automated message generated by Sweep AI.