Spaces:
Sleeping
Sleeping
cha0smagick
commited on
Commit
•
e814ac6
1
Parent(s):
5942d3d
Update app.py
Browse files
app.py
CHANGED
@@ -1,117 +1,33 @@
|
|
1 |
import streamlit as st
|
2 |
-
import
|
3 |
-
import
|
4 |
import google.generativeai as genai
|
5 |
-
from PIL import Image
|
6 |
|
7 |
-
|
|
|
8 |
|
9 |
-
|
|
|
|
|
10 |
|
|
|
11 |
|
12 |
-
|
13 |
-
# Clean punctuation and special characters using regular expressions
|
14 |
-
cleaned_text = re.sub(r'[^a-zA-Z0-9\s]', '', text)
|
15 |
-
return cleaned_text
|
16 |
|
17 |
|
18 |
-
|
19 |
-
|
20 |
|
21 |
-
|
22 |
-
|
23 |
-
response = model.generate_content(contents, image=image_file, stream=True)
|
24 |
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
full_response += chunk.text
|
29 |
|
30 |
-
|
31 |
-
|
32 |
-
except Exception as e:
|
33 |
-
error_message = str(e)
|
34 |
-
if "text must be a valid text with maximum 5000 characters" in error_message and not error_flag:
|
35 |
-
error_response = (
|
36 |
-
"The question you are asking may go against Google GEMINI policies: WiseOracle\n"
|
37 |
-
"Please reformulate your question without forbidden topics or ask something else. \n"
|
38 |
-
"For more information, see: https://policies.google.com/terms/generative-ai/use-policy "
|
39 |
-
)
|
40 |
-
st.error(error_response)
|
41 |
-
error_flag = True # Set the error_flag to True after displaying the error message
|
42 |
-
return error_response
|
43 |
-
elif "Add an image to use models/gemini-pro-vision, or switch your model to a text model" in error_message:
|
44 |
-
error_response = "Please upload an image to use the Gemini-Pro-Vision model."
|
45 |
-
st.error(error_response)
|
46 |
-
return error_response
|
47 |
-
else:
|
48 |
-
error_response = f"Error: {error_message}\nSorry, I am an artificial intelligence that is still in development and is in the alpha phase. At the moment, I cannot answer your question properly, but in the future, I will be able to do so."
|
49 |
-
st.error(error_response)
|
50 |
-
return error_response
|
51 |
-
|
52 |
-
|
53 |
-
def main():
|
54 |
-
st.title("WiseOracle")
|
55 |
-
genai.configure(api_key='AIzaSyAkbU3CsZ-xmOhRF1XfdlVxasRtt9gdRMk') # Replace with your Gemini API key
|
56 |
-
|
57 |
-
# Choose the Gemini model
|
58 |
model = genai.GenerativeModel('gemini-pro-vision')
|
|
|
59 |
|
60 |
-
|
61 |
-
|
62 |
-
# User input
|
63 |
-
user_input = st.text_input("Question:")
|
64 |
-
|
65 |
-
# Image upload
|
66 |
-
uploaded_file = st.file_uploader("Choose an image:", type=["jpg", "jpeg", "png"])
|
67 |
-
|
68 |
-
if st.button("Get answer"):
|
69 |
-
# Check if an image was uploaded
|
70 |
-
if not uploaded_file:
|
71 |
-
st.error("Please upload an image.")
|
72 |
-
return
|
73 |
-
|
74 |
-
# Save the image to a temporary file
|
75 |
-
with tempfile.NamedTemporaryFile(delete=False) as tmp_file:
|
76 |
-
tmp_file.write(uploaded_file.read())
|
77 |
-
|
78 |
-
# Preprocess the image
|
79 |
-
try:
|
80 |
-
img = Image.open(tmp_file.name)
|
81 |
-
img = img.resize((512, 512)) # Resize the image to 512x512
|
82 |
-
except Exception as e:
|
83 |
-
error_message = str(e)
|
84 |
-
error_response = f"Error: {error_message}\nPlease make sure you are uploading a valid image file."
|
85 |
-
st.error(error_response)
|
86 |
-
return
|
87 |
-
|
88 |
-
# Clean the user input of special characters
|
89 |
-
cleaned_input = clean_text(user_input)
|
90 |
-
|
91 |
-
# Exit if the cleaned text is empty
|
92 |
-
if not cleaned_input:
|
93 |
-
st.warning("Invalid input. Please try again.")
|
94 |
-
st.stop()
|
95 |
-
|
96 |
-
# Additional information about INIF
|
97 |
-
additional_info = (
|
98 |
-
"If you want to collaborate with the project, subscribe on Hugging Face or GitHub\n"
|
99 |
-
"If you want to donate, please donate in Bitcoin: 3KcF1yrY44smTJpVW68m8dw8q64kPtzvtX"
|
100 |
-
)
|
101 |
-
|
102 |
-
# Add the command to act as an INIF informative chatbot
|
103 |
-
bot_command = (
|
104 |
-
"I am an informative data analyst chatbot named WiseOracle, working for you as an assistant. "
|
105 |
-
"If you have questions about anything, feel free to ask."
|
106 |
-
f"\n\n{additional_info}"
|
107 |
-
)
|
108 |
-
|
109 |
-
# Generate the response
|
110 |
-
full_response = generate_response([bot_command, cleaned_input], model, [img, user_input])
|
111 |
-
|
112 |
-
# Display the generated response
|
113 |
-
st.success(full_response)
|
114 |
-
|
115 |
-
|
116 |
-
if __name__ == "__main__":
|
117 |
-
main()
|
|
|
1 |
import streamlit as st
|
2 |
+
import pathlib
|
3 |
+
import textwrap
|
4 |
import google.generativeai as genai
|
|
|
5 |
|
6 |
+
from IPython.display import display
|
7 |
+
from IPython.display import Markdown
|
8 |
|
9 |
+
def to_markdown(text):
|
10 |
+
text = text.replace('•', ' *')
|
11 |
+
return Markdown(textwrap.indent(text, '> ', predicate=lambda _: True))
|
12 |
|
13 |
+
GOOGLE_API_KEY = "AIzaSyAkbU3CsZ-xmOhRF1XfdlVxasRtt9gdRMk"
|
14 |
|
15 |
+
genai.configure(api_key=GOOGLE_API_KEY)
|
|
|
|
|
|
|
16 |
|
17 |
|
18 |
+
st.title("Gemini Image to Text App")
|
19 |
+
st.write("This app uses the Gemini API to generate text from images.")
|
20 |
|
21 |
+
# Get the image from the user
|
22 |
+
image = st.file_uploader("Upload an image")
|
|
|
23 |
|
24 |
+
if image is not None:
|
25 |
+
# Load the image
|
26 |
+
img = PIL.Image.open(image)
|
|
|
27 |
|
28 |
+
# Generate the text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
model = genai.GenerativeModel('gemini-pro-vision')
|
30 |
+
response = model.generate_content(img)
|
31 |
|
32 |
+
# Display the text
|
33 |
+
st.write(to_markdown(response.text))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|