cha0smagick commited on
Commit
56f6374
1 Parent(s): 2bd349a

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +104 -0
app.py ADDED
@@ -0,0 +1,104 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import re
3
+ import google.generativeai as genai
4
+ from PIL import Image
5
+ import io
6
+
7
+ # SAURONS EYE
8
+
9
+ error_flag = False # Global variable to track error display
10
+
11
+ def clean_text(text):
12
+ # Clean punctuation and special characters using regular expressions
13
+ cleaned_text = re.sub(r'[^a-zA-Z0-9\s]', '', text)
14
+ return cleaned_text
15
+
16
+
17
+ def generate_response(cleaned_input, model):
18
+ global error_flag # Use the global error_flag variable
19
+
20
+ try:
21
+ # Generate response using the model
22
+ response = model.generate_content(cleaned_input, stream=True)
23
+
24
+ # Display the generated response
25
+ full_response = ""
26
+ for chunk in response:
27
+ full_response += chunk.text
28
+
29
+ return full_response
30
+
31
+ except Exception as e:
32
+ error_message = str(e)
33
+ if "text must be a valid text with maximum 5000 character" in error_message and not error_flag:
34
+ error_response = ("The question you are asking may go against Google GEMINI policies: WiseOracle"
35
+ "Please reformulate your question without forbidden topics or ask something else. "
36
+ "For more information, see: https://policies.google.com/terms/generative-ai/use-policy ")
37
+ st.error(error_response)
38
+ error_flag = True # Set the error_flag to True after displaying the error message
39
+ return error_response
40
+ else:
41
+ error_response = f"Error: {error_message}\nSorry, I am an artificial intelligence that is still in development and is in alpha phase. At the moment, I cannot answer your question properly, but in the future, I will be able to do so."
42
+ st.error(error_response)
43
+ return error_response
44
+
45
+
46
+ def main():
47
+ st.title("WiseOracle")
48
+ genai.configure(api_key='your_google_api_key') # Replace with your Gemini API key
49
+
50
+ # Choose the Gemini model
51
+ model = genai.GenerativeModel('gemini-pro-vision')
52
+
53
+ st.write("Ask Anything! Powered by Google GEMINI")
54
+
55
+ # User input
56
+ user_input = st.text_input("Question:")
57
+
58
+ # Image upload
59
+ uploaded_file = st.file_uploader("Choose an image:")
60
+
61
+ if st.button("Get answer"):
62
+ if not uploaded_file:
63
+ st.error("Please upload an image.")
64
+ return
65
+
66
+ # Image preprocessing
67
+ img = Image.open(uploaded_file)
68
+ img = img.resize((512, 512)) # Resize the image to 512x512
69
+
70
+ # Convert image to bytes
71
+ buf = io.BytesIO()
72
+ img.save(buf, format='PNG')
73
+ byte_im = buf.getvalue()
74
+
75
+ # Clean the user input of special characters
76
+ cleaned_input = clean_text(user_input)
77
+
78
+ # Exit if the cleaned text is empty
79
+ if not cleaned_input:
80
+ st.warning("Invalid input. Please try again.")
81
+ st.stop()
82
+
83
+ # Additional information about INIF
84
+ additional_info = (
85
+ "If you want to collaborate with the project, subscribe on Hugging Face or GitHub\n"
86
+ "If you want to donate, please donate in Bitcoin: 3KcF1yrY44smTJpVW68m8dw8q64kPtzvtX"
87
+ )
88
+
89
+ # Add the command to act as an INIF informative chatbot
90
+ bot_command = (
91
+ "I am an informative data analyst chatbot named WiseOracle, working for you as an assistant. "
92
+ "If you have questions about anything, feel free to ask."
93
+ f"\n\n{additional_info}"
94
+ )
95
+
96
+ # Generate the response
97
+ full_response = generate_response(bot_command + cleaned_input, model)
98
+
99
+ # Display the generated response
100
+ st.success(full_response)
101
+
102
+
103
+ if __name__ == "__main__":
104
+ main()