-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathother.py
More file actions
174 lines (133 loc) 路 5.24 KB
/
Copy pathother.py
File metadata and controls
174 lines (133 loc) 路 5.24 KB
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# A file with functions that are to be used throughout the app
import streamlit as st
from google import genai
from openai import OpenAI
from groq import Groq
import base64
from google.genai import types
# Initialising the session states
def init_session():
defaults = {
"chat_history": [],
"chat_model": "groq-4",
"image_history": [],
"image_model": "gemini-2.5-flash",
"voice_history": [],
"voice_model": "gemini-2.5-flash",
"voice_mode": False,
"openai_api" : ""
}
for key, value in defaults.items():
if key not in st.session_state:
st.session_state[key] = value
# --------- CHAT RELATED FUNCTIONS -----------------------------------------------
# Adds chat message to the history of chats
def add_message(history_key, role, content):
st.session_state[history_key].append({"role": role, "content": content})
# Displays Chats
def show_chat(history_key):
for msg in st.session_state[history_key]:
with st.chat_message(msg["role"]):
st.markdown(msg["content"])
# Establishes connection with Gemini Api client
def get_gemini_client():
if "gemini_client" not in st.session_state:
st.session_state["gemini_client"] = genai.Client(api_key=st.secrets["GEMINI_API_KEY"])
return st.session_state["gemini_client"]
# Produces and returns response from gemini client
def gemini_chat(history):
client = get_gemini_client()
contents = "\n".join(f"{x} : {y}" for chat in history for x,y in chat.items())
response = client.models.generate_content(
model="gemini-2.5-flash",
contents=contents
)
return response.text
# Establishes Open AI API connection :
def get_openai_client():
if "openai_client" not in st.session_state:
st.session_state["openai_client"] = OpenAI(api_key=st.secrets["OPENAI_API_KEY"])
return st.session_state["openai_client"]
# Produces and returns response from OpenAI API
def openai_chat(history, model="gpt-4o"):
client = get_openai_client()
messages = []
for msg in history:
role = "user" if msg["role"] == "user" else "assistant"
messages.append({"role": role, "content": msg["content"]})
response = client.chat.completions.create(
model=model,
messages=messages
)
return response.choices[0].message.content
# Establishes connection with Groq API
def get_groq_client():
if "groq_client" not in st.session_state:
st.session_state["groq_client"] = Groq(api_key=st.secrets["GROQ_API_KEY"])
return st.session_state["groq_client"]
# Produces and returns response from Groq API
def groq_chat(history, model="groq-4"):
client = get_groq_client()
messages = []
for msg in history:
role = "user" if msg["role"] == "user" else "assistant"
messages.append({"role": role, "content": msg["content"]})
response = client.chat.completions.create(
model="groq/compound",
messages=messages
)
return response.choices[0].message.content
# ------------------- IMAGE RELATED FUNCTIONS ---------------------------------
# Image generation from prompt by dall-e-3 model
def openai_image(prompt):
client = get_openai_client()
response = client.images.generate(
model="dall-e-3",
prompt=prompt,
size="1024x1024"
)
return response.data[0].url
# Respond with the context from uploaded image
def gemini_img_chat(image, history):
client = get_gemini_client()
if image is None:
return "No Image uploaded !"
else :
binary_image = image.read()
response = client.models.generate_content(
model = "gemini-2.5-flash",
contents=[
genai.types.Part.from_bytes(
data=binary_image,
mime_type='image/jpeg',
), history
]
)
return response.text
# ------------------------ THEME RELATED FUNCTIONS ------------------------------------
# Initialise the session state for theme
def init_theme():
if "theme_mode" not in st.session_state:
st.session_state.theme_mode = "light"
# Change theme after toggle
def toggle_theme():
if st.session_state.theme_mode == "light":
st.session_state.theme_mode = "dark"
else:
st.session_state.theme_mode = "light"
apply_theme()
# Set configuration of the theme
def apply_theme():
mode = st.session_state.theme_mode
if mode == "dark":
st._config.set_option("theme.base", "dark")
st._config.set_option("theme.backgroundColor", "#0e1117")
st._config.set_option("theme.primaryColor", "#83FDB2")
st._config.set_option("theme.secondaryBackgroundColor", "#262730")
st._config.set_option("theme.textColor", "#ffffff")
else:
st._config.set_option("theme.base", "light")
st._config.set_option("theme.backgroundColor", "white")
st._config.set_option("theme.primaryColor", "#59a2d6")
st._config.set_option("theme.secondaryBackgroundColor", "#f0f2f6")
st._config.set_option("theme.textColor", "#000000")