Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,80 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from transformers import pipeline
|
2 |
+
import streamlit as st
|
3 |
+
from PIL import Image
|
4 |
+
import requests
|
5 |
+
from io import BytesIO
|
6 |
+
from sentence_transformers import SentenceTransformer
|
7 |
+
import numpy as np
|
8 |
+
import faiss
|
9 |
+
import pandas as pd
|
10 |
+
|
11 |
+
# Initialize the image-to-text pipeline
|
12 |
+
image_to_text = pipeline("image-to-text", model="nlpconnect/vit-gpt2-image-captioning")
|
13 |
+
|
14 |
+
# Initialize the sentence transformer model for embeddings
|
15 |
+
sentence_model = SentenceTransformer('sentence-transformers/all-MiniLM-L6-v2')
|
16 |
+
|
17 |
+
# Function to load images from URL
|
18 |
+
def load_image_from_url(url):
|
19 |
+
try:
|
20 |
+
response = requests.get(url)
|
21 |
+
img = Image.open(BytesIO(response.content))
|
22 |
+
return img
|
23 |
+
except Exception as e:
|
24 |
+
st.error(f"Error loading image from URL: {e}")
|
25 |
+
return None
|
26 |
+
|
27 |
+
# Load the dataset and create FAISS index
|
28 |
+
def load_dataset_and_create_index():
|
29 |
+
df = pd.read_csv('/path/to/your/amazon_reviews.csv')
|
30 |
+
review_texts = df['reviewText'].dropna().tolist()
|
31 |
+
review_embeddings = sentence_model.encode(review_texts)
|
32 |
+
dimension = review_embeddings.shape[1]
|
33 |
+
faiss_index = faiss.IndexFlatL2(dimension)
|
34 |
+
faiss_index.add(np.array(review_embeddings))
|
35 |
+
return faiss_index, review_texts
|
36 |
+
|
37 |
+
faiss_index, review_texts = load_dataset_and_create_index()
|
38 |
+
|
39 |
+
# Find top N similar reviews
|
40 |
+
def find_top_n_similar_reviews(query, faiss_index, review_texts, top_n=3):
|
41 |
+
query_embedding = sentence_model.encode([query])
|
42 |
+
_, indices = faiss_index.search(query_embedding, top_n)
|
43 |
+
return [review_texts[i] for i in indices[0]]
|
44 |
+
|
45 |
+
st.title('Image Captioning and Review Visualization Application')
|
46 |
+
|
47 |
+
input_type = st.radio("Select input type:", ("Upload Image", "Image URL", "Text"))
|
48 |
+
|
49 |
+
image = None
|
50 |
+
text_input = ""
|
51 |
+
|
52 |
+
if input_type == "Upload Image":
|
53 |
+
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
|
54 |
+
if uploaded_file is not None:
|
55 |
+
image = Image.open(uploaded_file)
|
56 |
+
st.image(image, caption='Uploaded Image', use_column_width=True)
|
57 |
+
elif input_type == "Image URL":
|
58 |
+
image_url = st.text_input("Enter the image URL here:", "")
|
59 |
+
if image_url:
|
60 |
+
image = load_image_from_url(image_url)
|
61 |
+
elif input_type == "Text":
|
62 |
+
text_input = st.text_area("Enter text here:", "")
|
63 |
+
|
64 |
+
if st.button('Generate Caption'):
|
65 |
+
result_text = ""
|
66 |
+
if input_type in ["Upload Image", "Image URL"] and image:
|
67 |
+
with st.spinner("Generating caption..."):
|
68 |
+
result = image_to_text(image_url if input_type == "Image URL" else uploaded_file)
|
69 |
+
result_text = result[0]['generated_text'] if result else "Failed to generate caption."
|
70 |
+
elif input_type == "Text" and text_input:
|
71 |
+
result_text = text_input
|
72 |
+
|
73 |
+
if result_text:
|
74 |
+
st.success(f'Generated Caption: {result_text}')
|
75 |
+
similar_reviews = find_top_n_similar_reviews(result_text, faiss_index, review_texts)
|
76 |
+
st.write("Similar Reviews Based on the Caption:")
|
77 |
+
for review in similar_reviews:
|
78 |
+
st.write(review)
|
79 |
+
else:
|
80 |
+
st.error("Please provide input.")
|