Spaces:
Runtime error
Runtime error
File size: 2,939 Bytes
696bc12 48fbb0a 696bc12 48fbb0a 696bc12 5af2396 696bc12 a2e53e1 696bc12 bafb4f2 696bc12 69d7f05 7fd1106 aa91ec0 69d7f05 aa91ec0 69d7f05 a8a89b9 9f6023f 696bc12 69d7f05 696bc12 7fd1106 696bc12 aa91ec0 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 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
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")
CX_KEY = os.getenv("CX_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=CX_KEY,
num=num,
)
.execute()
)
links = ""
if res['items'] is None or len(res['items']) == 0:
return "There was a problem with the query"
for item in res['items']:
links += f"- [{item['title']}]({item['link']})\n"
return links
def remove_quotes(input_string):
return input_string.replace('"', '')
import re
def append_tag_to_amazon_url(input_string):
pattern = r'(https?://www\.amazon\.com[^)]*)'
def append_tag(match):
url = match.group(0)
if '?' in url:
return url + '&tag=dpang-20'
else:
return url + '?tag=dpang-20'
return re.sub(pattern, append_tag, input_string)
# Streamlit interface
st.title('Vision Shop - shopping via images')
url = st.text_input('Enter the url link to an image ( Example: https://images.ctfassets.net/7rldri896b2a/4augh14at0OZJuEhbWF0av/09dd54fe6543a36f2832f79cc51adad1/spc-bathdecor.jpg )', '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)
search_str = remove_quotes(search_str)
amazon_output = search_amazon(search_str)
processed_lines = [append_tag_to_amazon_url(line) for line in amazon_output.split('\n')]
output_str = '\n'.join(processed_lines)
print(output_str)
st.write(output_str)
#print(amazon_output)
#st.write(amazon_output)
except Exception as e:
st.error(f'An error occurred: {e}')
|