File size: 1,975 Bytes
696bc12
48fbb0a
696bc12
 
 
48fbb0a
696bc12
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
69d7f05
 
 
696bc12
 
 
69d7f05
 
696bc12
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
69d7f05
 
696bc12
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
from googleapiclient.discovery import build
import streamlit as st
import requests
from openai import OpenAI
import os

client = OpenAI()
client.key = os.getenv("OPENAI_API_KEY")
GOOGLE_API_DEV_KEY = os.getenv("GOOGLE_API_DEV_KEY")


def search_amazon(query: str, num: int = 10) -> str:
    service = build(
        "customsearch", "v1", developerKey=GOOGLE_API_DEV_KEY
    )

    res = (
        service.cse()
        .list(
            q=query,
            cx="103114c8487ce4aa1",
            num=num,
        )
        .execute()
    )

    links = ""

    for item in res['items']:
        links += f"- [{item['title']}]({item['link']})\n"

    return links


# Streamlit interface
st.title('Vision Hop')
url = st.text_input('Enter the image URL', 'Image URL')
if st.button('Shop'):
    # Make a POST request
    try:
        response = client.chat.completions.create(
            model="gpt-4-vision-preview",
            messages=[
                {
                    "role": "user",
                    "content": [
                        {"type": "text",
                            "text": "Summarize the image as a search string for Amazon.com"},
#                         "text": "Identify all the items available to be purchased and for each item list the search terms you would to find them on Amazon.com.  Put a number before the search terms"},

                        {
                            "type": "image_url",
                            "image_url": {
                                "url": url,
                            },
                        },
                    ],
                }
            ],
            max_tokens=300,
        )
        search_str = response.choices[0].message.content
        print(search_str)
        st.write(search_str)

        amazon_output = search_amazon(search_str)
        print(amazon_output)
        st.write(amazon_output)

    except Exception as e:
        st.error(f'An error occurred: {e}')