ferdmartin commited on
Commit
723f0ac
·
verified ·
1 Parent(s): 2f81a33

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +76 -0
app.py ADDED
@@ -0,0 +1,76 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import json
3
+ import numpy as np
4
+ import tensorflow as tf
5
+ import gdown
6
+ import zipfile
7
+
8
+ # Cache the model loading
9
+ @st.cache(allow_output_mutation=True)
10
+ def load_model():
11
+ # The shareable link to your Google Drive file
12
+ url = "https://drive.google.com/uc?id=1m9YVs0cBRT3-j98rn7d_0DT7jwB_EXPu"
13
+ output = 'model.zip'
14
+ gdown.download(url, output, quiet=False)
15
+
16
+ with zipfile.ZipFile(output, 'r') as zip_ref:
17
+ zip_ref.extractall(".")
18
+
19
+ model = tf.keras.models.load_model('best_model')
20
+ return model
21
+
22
+ # Cache the JSON data loading
23
+ @st.cache(allow_output_mutation=True)
24
+ def load_json(filename):
25
+ with open(filename, 'r') as f:
26
+ return json.load(f)
27
+
28
+ user_db = load_json("user_db.json")
29
+ item_db = load_json("item_db.json")
30
+
31
+ # Function to predict rating
32
+ def predict_rating(reviewerID, itemID, model):
33
+ item_attributes = item_db.get(itemID, {})
34
+ user_attributes = user_db.get(reviewerID, {})
35
+
36
+ category = item_attributes.get('category', 4) # Assuming default values
37
+ price = item_attributes.get('price', 13.71)
38
+ userAvgRating = user_attributes.get('userAvgRating', 4)
39
+ itemAvgRating = item_attributes.get('itemAvgRating', 4)
40
+ review_time = user_attributes.get('unixReviewTime', 1285579290)
41
+
42
+ prediction_inputs = {
43
+ 'reviewer_id': np.array([[reviewerID]]),
44
+ 'item_id': np.array([[itemID]]),
45
+ 'category': np.array([[category]]),
46
+ 'price': np.array([[price]]),
47
+ 'paid_price': np.array([[price]]),
48
+ "unixReviewTime": np.array([[review_time]]),
49
+ 'userAvgRating': np.array([[userAvgRating]]),
50
+ 'itemAvgRating': np.array([[itemAvgRating]]),
51
+ 'review_text': np.array([["placeholder text"]]),
52
+ 'summary': np.array([["placeholder summary"]]),
53
+ }
54
+
55
+ # Transform the input dictionary as needed to match your model's expected input format
56
+ prediction = model.predict(prediction_inputs)
57
+ return prediction[0][0]
58
+
59
+ # Load the model
60
+ model = load_model()
61
+
62
+ # Streamlit app interface
63
+ st.title('Product Rating Prediction')
64
+
65
+ # Example input values
66
+ example_reviewerID = "A1YS9MDZP93857" # Example reviewerID
67
+ example_itemID = "B001GVISJM" # Example itemID
68
+
69
+ # User inputs with examples
70
+ reviewerID = st.text_input('Reviewer ID', value=example_reviewerID)
71
+ itemID = st.text_input('Item ID', value=example_itemID)
72
+
73
+ # Predict button
74
+ if st.button('Predict Rating'):
75
+ prediction = predict_rating(reviewerID, itemID, model)
76
+ st.write(f'Predicted Rating: {prediction:.2f}')