linm1 commited on
Commit
76e65f9
1 Parent(s): 2f8dab0

Update app.py

Browse files

use Mongo DB to collect feedback.

Files changed (1) hide show
  1. app.py +28 -26
app.py CHANGED
@@ -9,11 +9,21 @@ from PIL import Image
9
  import json
10
  import sqlite3
11
  from datetime import datetime
 
12
 
13
  # Load the dataset
14
  ds = load_dataset("svjack/pokemon-blip-captions-en-zh")
15
  ds = ds["train"]
16
 
 
 
 
 
 
 
 
 
 
17
  # Load environment variables
18
  api_key = os.environ.get('MISTRAL_API_KEY')
19
 
@@ -87,18 +97,21 @@ def generate_caption(image):
87
  return None
88
 
89
  # Initialize SQLite database
90
- def init_db():
91
- conn = sqlite3.connect('feedback.db')
92
- c = conn.cursor()
93
- c.execute('''CREATE TABLE IF NOT EXISTS thumbs_up
94
- (id INTEGER PRIMARY KEY AUTOINCREMENT,
95
- timestamp TEXT,
96
- input_data TEXT,
97
- output_data TEXT)''')
98
- conn.commit()
99
- conn.close()
100
 
101
- init_db()
 
 
 
 
 
 
 
 
102
 
103
  def process_image(image):
104
  if image is None:
@@ -112,23 +125,12 @@ def process_image(image):
112
  return "Failed to generate caption. Please check the API call or network connectivity."
113
 
114
  def thumbs_up(image, caption):
115
- # Convert image to base64 string for storage
116
- buffered = BytesIO()
117
- image.save(buffered, format="JPEG")
118
- img_str = base64.b64encode(buffered.getvalue()).decode()
119
-
120
- conn = sqlite3.connect('feedback.db')
121
- c = conn.cursor()
122
- c.execute("INSERT INTO thumbs_up (timestamp, input_data, output_data) VALUES (?, ?, ?)",
123
- (datetime.now().isoformat(), img_str, caption))
124
- conn.commit()
125
- conn.close()
126
- print(f"Thumbs up data saved to database.")
127
- return gr.Info("Thank you for your feedback!")
128
 
129
  with gr.Blocks() as iface:
130
  gr.Markdown("# Image Captioner")
131
- gr.Markdown("Upload an image to generate captions in English and Chinese. Use the 'Thumbs Up' button if you like the result!")
 
132
 
133
  with gr.Row():
134
  with gr.Column(scale=1):
@@ -146,4 +148,4 @@ with gr.Blocks() as iface:
146
  thumbs_up_btn.click(fn=thumbs_up, inputs=[input_image, output_text], outputs=None)
147
 
148
  # Launch the interface
149
- iface.launch(share=True)
 
9
  import json
10
  import sqlite3
11
  from datetime import datetime
12
+ from pymongo import MongoClient
13
 
14
  # Load the dataset
15
  ds = load_dataset("svjack/pokemon-blip-captions-en-zh")
16
  ds = ds["train"]
17
 
18
+ # load MongoDB client
19
+ MONGO_URI = os.environ.get('MONGO_URI')
20
+ if not MONGO_URI:
21
+ raise ValueError("MONGO_URI is not set in the environment variables.")
22
+
23
+ client = MongoClient(MONGO_URI)
24
+ db = client['capimg'] # Choose a database name
25
+ collection = db['feedback'] # Choose a collection name
26
+
27
  # Load environment variables
28
  api_key = os.environ.get('MISTRAL_API_KEY')
29
 
 
97
  return None
98
 
99
  # Initialize SQLite database
100
+ def save_feedback(image, caption):
101
+ # Convert image to base64 string for storage
102
+ buffered = BytesIO()
103
+ image.save(buffered, format="JPEG")
104
+ img_str = base64.b64encode(buffered.getvalue()).decode()
 
 
 
 
 
105
 
106
+ feedback_entry = {
107
+ "timestamp": datetime.now(),
108
+ "input_data": img_str,
109
+ "output_data": caption
110
+ }
111
+
112
+ result = collection.insert_one(feedback_entry)
113
+ print(f"Feedback saved with id: {result.inserted_id}")
114
+ return gr.Info("Thanks for your feedback!")
115
 
116
  def process_image(image):
117
  if image is None:
 
125
  return "Failed to generate caption. Please check the API call or network connectivity."
126
 
127
  def thumbs_up(image, caption):
128
+ return save_feedback(image, caption)
 
 
 
 
 
 
 
 
 
 
 
 
129
 
130
  with gr.Blocks() as iface:
131
  gr.Markdown("# Image Captioner")
132
+ gr.Markdown("Upload an image to generate captions in English and Chinese.")
133
+ gr.Markdown("Use the 'Thumbs Up' button if you like the result!!")
134
 
135
  with gr.Row():
136
  with gr.Column(scale=1):
 
148
  thumbs_up_btn.click(fn=thumbs_up, inputs=[input_image, output_text], outputs=None)
149
 
150
  # Launch the interface
151
+ iface.launch()