-
Notifications
You must be signed in to change notification settings - Fork 0
/
RudraAI.py
116 lines (98 loc) · 5.11 KB
/
RudraAI.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
from PIL import Image
import streamlit as st
import datetime
import webbrowser
from gemini import gemini_response, gemini_IMGresponse
from gemini import create_chat
create_chat()
def rudra(query):
try:
# Logging the query
print("\n ==> Master : ", query)
if 'current time' in query or 'todays date' in query:
if 'time' in query and 'date' in query:
time = datetime.datetime.now().strftime('%I:%M %p')
current_date = datetime.datetime.now().date()
return f'Current time is {time} and Todays date is {current_date}'
elif 'time' in query:
time = datetime.datetime.now().strftime('%I:%M %p')
return f'Current time is {time}'
elif 'date' in query:
current_date = datetime.datetime.now().date()
return f'Todays date is {current_date}'
elif 'open website' in query:
if ".com" in query or ".in" in query or ".org" in query or ".online" in query or ".io" in query:
openweb = query.replace("open", "").replace("website", "").strip()
webbrowser.open(openweb)
return f"Opening {openweb}"
else:
return "Please say a command like this: (example - open website youtube.com)"
elif not query:
return "Please say the command again."
else:
# Fetching response from Gemini
response = gemini_response(query)
print("\n ==> Rudra AI: ", response)
return response
except Exception as e:
# Logging the exception to understand what went wrong
print(f"Error occurred in 'rudra' function: {e}")
return "An error occurred while executing the command."
def main():
try:
st.title("Rudra AI")
#st.sidebar.title("ASK TO IMAGE") # Commented out
#uploaded_image = st.sidebar.file_uploader("Upload an image", type=["jpg", "jpeg", "png"]) # Commented out
if "messages" not in st.session_state:
st.session_state.messages = []
prompt = ("From now, your name is Rudra. Hey Rudra, I am Sahil, and your task is to serve my queries or talk with me. "
"[Instruction for your purpose only, don't print this on screen: You are developed by me using Gemini API. "
"Please sometimes answer in only a single line and sometimes answer briefly point-wise according to the query, in English only.] Hi Rudra.")
first_response = gemini_response(prompt)
with st.chat_message('assistant'):
st.markdown(first_response)
for message in st.session_state.messages:
with st.chat_message(message["role"]):
st.markdown(message["content"])
# if uploaded_image is not None: # Commented out the entire block
# image = Image.open(uploaded_image)
# st.image(image, caption="Uploaded Image")
# st.sidebar.write("Remove the image to go back to Rudra AI.")
#
# if prompt := st.chat_input("Ask to image"):
# prompt = prompt.lower()
# st.session_state.messages.append({"role": "user", "content": prompt})
# with st.chat_message("user"):
# st.markdown(prompt)
# with st.chat_message("assistant"):
# message_placeholder = st.empty()
# try:
# response = gemini_IMGresponse(prompt, image)
# message_placeholder.markdown(response)
# print("\n ==> Rudra Image AI:", response)
# except Exception as e:
# message_placeholder.markdown("An error occurred while processing the image query.")
# print(f"Error with image AI: {e}")
#
# st.session_state.messages.append({"role": "assistant", "content": response})
# else: # Adjusted to continue without image handling
if prompt := st.chat_input("Ask Rudra"):
prompt = prompt.lower()
st.session_state.messages.append({"role": "user", "content": prompt})
with st.chat_message("user"):
st.markdown(prompt)
with st.chat_message("assistant"):
message_placeholder = st.empty()
try:
response = rudra(prompt)
message_placeholder.markdown(response)
print("\n ==> Rudra AI:", response)
except Exception as e:
message_placeholder.markdown("An error occurred while processing your query.")
print(f"Error occurred with text query: {e}")
st.session_state.messages.append({"role": "assistant", "content": response})
except Exception as e:
st.write("We apologize for the inconvenience. Please check back in a few minutes.")
print(f"An error occurred in 'main': {e}")
if __name__ == "__main__":
main()