witchEverly
commited on
Update app_utils.py
Browse files- app_utils.py +29 -26
app_utils.py
CHANGED
@@ -1,17 +1,20 @@
|
|
1 |
"""
|
2 |
Utility functions for the Instagram Caption Generator app.
|
3 |
"""
|
4 |
-
import streamlit as st
|
5 |
-
from dotenv import load_dotenv
|
6 |
-
from transformers import AutoProcessor, Blip2ForConditionalGeneration
|
7 |
import os
|
8 |
from pathlib import Path
|
|
|
|
|
9 |
import pandas as pd
|
|
|
|
|
10 |
|
11 |
|
12 |
def get_gemini_api_key():
|
13 |
"""
|
14 |
-
|
|
|
|
|
15 |
:return: str - The Google API key.
|
16 |
"""
|
17 |
load_dotenv()
|
@@ -20,24 +23,21 @@ def get_gemini_api_key():
|
|
20 |
|
21 |
|
22 |
@st.cache_resource()
|
23 |
-
def init_model(
|
24 |
"""
|
25 |
Initializes the BLIP-2 model and processor for image captioning.
|
26 |
-
|
27 |
The streamlit app can call this function to load the model and processor
|
28 |
-
|
29 |
-
:param init_model_required:
|
30 |
-
ol - Flag to indicate if the model needs to be initialized.
|
31 |
:returns: AutoProcessor, Blip2ForConditionalGeneration, bool - Model processor, BLIP-2 model, and flag.
|
32 |
"""
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
except Exception as e:
|
40 |
-
st.error(f"Error occurred during model initialization: {e}")
|
41 |
|
42 |
|
43 |
# Function to store the user data to a CSV file
|
@@ -57,24 +57,27 @@ def save_user_data(first_name, last_name, email, phone):
|
|
57 |
df = pd.read_csv(csv_file)
|
58 |
else:
|
59 |
df = pd.DataFrame(columns=["First Name", "Last Name", "Email", "Phone Number"])
|
60 |
-
|
61 |
-
|
62 |
-
new_data = pd.DataFrame({"First Name": [first_name],
|
63 |
-
"Last Name": [last_name],
|
64 |
-
"Email": [email],
|
65 |
-
"Phone Number": [phone]})
|
66 |
-
df = pd.concat([df, new_data], ignore_index=True)
|
67 |
df.to_csv(csv_file, index=False)
|
68 |
return None
|
69 |
|
70 |
|
71 |
def get_gif(path):
|
72 |
-
"""
|
|
|
|
|
|
|
|
|
73 |
with open(path, "rb") as file:
|
74 |
gif = file.read()
|
75 |
return gif
|
76 |
|
77 |
|
78 |
# Blip-2 does most of the standard image processing needed for image captioning.
|
79 |
-
def process_image(
|
|
|
|
|
|
|
|
|
80 |
pass
|
|
|
1 |
"""
|
2 |
Utility functions for the Instagram Caption Generator app.
|
3 |
"""
|
|
|
|
|
|
|
4 |
import os
|
5 |
from pathlib import Path
|
6 |
+
|
7 |
+
from dotenv import load_dotenv
|
8 |
import pandas as pd
|
9 |
+
import streamlit as st
|
10 |
+
from transformers import AutoProcessor, Blip2ForConditionalGeneration
|
11 |
|
12 |
|
13 |
def get_gemini_api_key():
|
14 |
"""
|
15 |
+
The api key is stored in as a private environment variable,
|
16 |
+
the purpose of this function is to retrieve the Google API key
|
17 |
+
for accessing the Generative AI API.
|
18 |
:return: str - The Google API key.
|
19 |
"""
|
20 |
load_dotenv()
|
|
|
23 |
|
24 |
|
25 |
@st.cache_resource()
|
26 |
+
def init_model():
|
27 |
"""
|
28 |
Initializes the BLIP-2 model and processor for image captioning.
|
29 |
+
The cache_resource decorator is used to cache the model and processor.
|
30 |
The streamlit app can call this function to load the model and processor
|
31 |
+
without reinitializing it.
|
32 |
+
:param init_model_required: bool - Flag to indicate if the model needs to be initialized.
|
|
|
33 |
:returns: AutoProcessor, Blip2ForConditionalGeneration, bool - Model processor, BLIP-2 model, and flag.
|
34 |
"""
|
35 |
+
try:
|
36 |
+
processor = AutoProcessor.from_pretrained(os.path.expanduser('~/data/pretrained/blip2-opt-2.7b'))
|
37 |
+
blip2_model = Blip2ForConditionalGeneration.from_pretrained(os.path.expanduser('~/data/pretrained/blip2-opt-2.7b'))
|
38 |
+
return processor, blip2_model
|
39 |
+
except Exception as e:
|
40 |
+
st.error(f"Error occurred during model initialization: {e}")
|
|
|
|
|
41 |
|
42 |
|
43 |
# Function to store the user data to a CSV file
|
|
|
57 |
df = pd.read_csv(csv_file)
|
58 |
else:
|
59 |
df = pd.DataFrame(columns=["First Name", "Last Name", "Email", "Phone Number"])
|
60 |
+
new_data = {"First Name": first_name, "Last Name": last_name, "Email": email, "Phone Number": phone}
|
61 |
+
df = df.append(new_data, ignore_index=True)
|
|
|
|
|
|
|
|
|
|
|
62 |
df.to_csv(csv_file, index=False)
|
63 |
return None
|
64 |
|
65 |
|
66 |
def get_gif(path):
|
67 |
+
"""
|
68 |
+
Function to get the GIF image from the specified path.
|
69 |
+
:param path: str - Path to the GIF image
|
70 |
+
:return: bytes - The GIF image
|
71 |
+
"""
|
72 |
with open(path, "rb") as file:
|
73 |
gif = file.read()
|
74 |
return gif
|
75 |
|
76 |
|
77 |
# Blip-2 does most of the standard image processing needed for image captioning.
|
78 |
+
def process_image():
|
79 |
+
"""
|
80 |
+
Unused function for image processing,
|
81 |
+
not needed for the current implementation.
|
82 |
+
"""
|
83 |
pass
|