Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,115 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from googleapiclient.discovery import build
|
2 |
+
import streamlit as st
|
3 |
+
import requests
|
4 |
+
from openai import OpenAI
|
5 |
+
import os
|
6 |
+
import bentoml
|
7 |
+
from pathlib import Path
|
8 |
+
|
9 |
+
client = OpenAI()
|
10 |
+
client.key = os.getenv("OPENAI_API_KEY")
|
11 |
+
GOOGLE_API_DEV_KEY = os.getenv("GOOGLE_API_DEV_KEY")
|
12 |
+
|
13 |
+
|
14 |
+
def search_amazon(query: str, num: int = 10) -> str:
|
15 |
+
service = build(
|
16 |
+
"customsearch", "v1", developerKey=GOOGLE_API_DEV_KEY
|
17 |
+
)
|
18 |
+
|
19 |
+
res = (
|
20 |
+
service.cse()
|
21 |
+
.list(
|
22 |
+
q=query,
|
23 |
+
cx="103114c8487ce4aa1",
|
24 |
+
num=num,
|
25 |
+
)
|
26 |
+
.execute()
|
27 |
+
)
|
28 |
+
|
29 |
+
links = ""
|
30 |
+
|
31 |
+
if res['items'] is None or res['items'] == []:
|
32 |
+
return "No items found"
|
33 |
+
for item in res['items']:
|
34 |
+
links += f"- [{item['title']}]({item['link']})\n"
|
35 |
+
|
36 |
+
return links
|
37 |
+
|
38 |
+
def remove_quotes(input_string):
|
39 |
+
return input_string.replace('"', '')
|
40 |
+
|
41 |
+
import re
|
42 |
+
|
43 |
+
def append_tag_to_amazon_url(input_string):
|
44 |
+
pattern = r'(https?://www\.amazon\.com[^)]*)'
|
45 |
+
|
46 |
+
def append_tag(match):
|
47 |
+
url = match.group(0)
|
48 |
+
if '?' in url:
|
49 |
+
return url + '&tag=dpang-20'
|
50 |
+
else:
|
51 |
+
return url + '?tag=dpang-20'
|
52 |
+
|
53 |
+
return re.sub(pattern, append_tag, input_string)
|
54 |
+
|
55 |
+
import requests
|
56 |
+
import os
|
57 |
+
|
58 |
+
def downloadImage(image_url):
|
59 |
+
|
60 |
+
# Extract the file name and extension from the URL
|
61 |
+
file_name = os.path.basename(image_url)
|
62 |
+
|
63 |
+
# Local file path where you want to save the image
|
64 |
+
local_file_path = os.path.join('./', file_name)
|
65 |
+
|
66 |
+
# Send a GET request to the URL
|
67 |
+
response = requests.get(image_url)
|
68 |
+
|
69 |
+
# Check if the request was successful (status code 200)
|
70 |
+
if response.status_code == 200:
|
71 |
+
# Open a local file in write-binary ('wb') mode
|
72 |
+
with open(local_file_path, 'wb') as file:
|
73 |
+
# Write the content of the response to the file
|
74 |
+
file.write(response.content)
|
75 |
+
print(f"Image downloaded and saved as {local_file_path}")
|
76 |
+
else:
|
77 |
+
print(f"Failed to download the image. Status code: {response.status_code}")
|
78 |
+
|
79 |
+
return file_name
|
80 |
+
|
81 |
+
# Streamlit interface
|
82 |
+
st.title('Vision SHop')
|
83 |
+
url = st.text_input('Enter the url link to an image ( Example: https://images.ctfassets.net/7rldri896b2a/4augh14at0OZJuEhbWF0av/09dd54fe6543a36f2832f79cc51adad1/spc-bathdecor.jpg )', '')
|
84 |
+
#url = st.text_input('Enter the url link to the image', 'Image URL')
|
85 |
+
if st.button('Shop'):
|
86 |
+
# Make a POST request
|
87 |
+
try:
|
88 |
+
basename=downloadImage(url)
|
89 |
+
result=""
|
90 |
+
with bentoml.SyncHTTPClient("https://blip-image-captioning-r2hq-org-rag-hackathon--gcp-us-central-1.mt-guc1.bentoml.ai") as client:
|
91 |
+
result = client.generate(
|
92 |
+
# img=Path("https://assets.wfcdn.com/im/59302811/resize-h1500-w1500%5Ecompr-r85/1882/188248294/Window+Scenery+Green+Peaceful+Lake+Natural+Landscape+Photography+Pictures+Canvas+Print+Wall+Art.jpg"),
|
93 |
+
img=Path(basename),
|
94 |
+
txt="",
|
95 |
+
)
|
96 |
+
|
97 |
+
search_str = result
|
98 |
+
print(search_str)
|
99 |
+
st.write(search_str)
|
100 |
+
|
101 |
+
search_str = remove_quotes(search_str)
|
102 |
+
|
103 |
+
amazon_output = search_amazon(search_str)
|
104 |
+
|
105 |
+
processed_lines = [append_tag_to_amazon_url(line) for line in amazon_output.split('\n')]
|
106 |
+
output_str = '\n'.join(processed_lines)
|
107 |
+
|
108 |
+
print(output_str)
|
109 |
+
st.write(output_str)
|
110 |
+
|
111 |
+
except Exception as e:
|
112 |
+
st.error(f'An error occurred: {e}')
|
113 |
+
|
114 |
+
|
115 |
+
|