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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
|
3 |
+
|
4 |
+
import streamlit as st
|
5 |
+
import os
|
6 |
+
from openai import OpenAI
|
7 |
+
from dotenv import load_dotenv
|
8 |
+
load_dotenv()
|
9 |
+
import base64
|
10 |
+
|
11 |
+
load_dotenv()
|
12 |
+
|
13 |
+
def get_together_models():
|
14 |
+
return [
|
15 |
+
"meta-llama/Meta-Llama-3-8B-Instruct-Turbo",
|
16 |
+
"meta-llama/Meta-Llama-3-70B-Instruct-Lite",
|
17 |
+
"meta-llama/Meta-Llama-3.1-405B-Instruct-Turbo",
|
18 |
+
"google/gemma-2-9b-it",
|
19 |
+
"meta-llama/Meta-Llama-3.1-70B-Instruct-Turbo",
|
20 |
+
"deepseek-ai/deepseek-coder-33b-instruct",
|
21 |
+
"meta-llama/Meta-Llama-3-70B-Instruct-Turbo",
|
22 |
+
"meta-llama/Meta-Llama-3.1-8B-Instruct-Turbo",
|
23 |
+
"meta-llama/Meta-Llama-3-8B-Instruct-Lite",
|
24 |
+
"meta-llama/Meta-Llama-3-70B-Instruct-Lite",
|
25 |
+
"google/gemma-2-27b-it",
|
26 |
+
"allenai/OLMo-7B-Instruct",
|
27 |
+
"zero-one-ai/Yi-34B-Chat",
|
28 |
+
"allenai/OLMo-7B-Twin-2T",
|
29 |
+
"allenai/OLMo-7B",
|
30 |
+
"Austism/chronos-hermes-13b",
|
31 |
+
"cognitivecomputations/dolphin-2.5-mixtral-8x7b",
|
32 |
+
"databricks/dbrx-instruct",
|
33 |
+
|
34 |
+
"deepseek-ai/deepseek-llm-67b-chat",
|
35 |
+
"garage-bAInd/Platypus2-70B-instruct",
|
36 |
+
"google/gemma-2b-it",
|
37 |
+
"google/gemma-7b-it",
|
38 |
+
"Gryphe/MythoMax-L2-13b",
|
39 |
+
"lmsys/vicuna-13b-v1.5",
|
40 |
+
"lmsys/vicuna-7b-v1.5",
|
41 |
+
"codellama/CodeLlama-13b-Instruct-hf",
|
42 |
+
"codellama/CodeLlama-34b-Instruct-hf",
|
43 |
+
"codellama/CodeLlama-70b-Instruct-hf",
|
44 |
+
"codellama/CodeLlama-7b-Instruct-hf",
|
45 |
+
"meta-llama/Llama-2-70b-chat-hf",
|
46 |
+
"meta-llama/Llama-2-13b-chat-hf",
|
47 |
+
"meta-llama/Llama-2-7b-chat-hf",
|
48 |
+
"meta-llama/Llama-3-8b-chat-hf",
|
49 |
+
"meta-llama/Llama-3-70b-chat-hf",
|
50 |
+
"mistralai/Mistral-7B-Instruct-v0.1",
|
51 |
+
"mistralai/Mistral-7B-Instruct-v0.2",
|
52 |
+
"mistralai/Mistral-7B-Instruct-v0.3",
|
53 |
+
"mistralai/Mixtral-8x7B-Instruct-v0.1",
|
54 |
+
"mistralai/Mixtral-8x22B-Instruct-v0.1",
|
55 |
+
"NousResearch/Nous-Capybara-7B-V1p9",
|
56 |
+
"NousResearch/Nous-Hermes-2-Mistral-7B-DPO",
|
57 |
+
"NousResearch/Nous-Hermes-2-Mixtral-8x7B-DPO",
|
58 |
+
"NousResearch/Nous-Hermes-2-Mixtral-8x7B-SFT",
|
59 |
+
"NousResearch/Nous-Hermes-llama-2-7b",
|
60 |
+
"NousResearch/Nous-Hermes-Llama2-13b",
|
61 |
+
"NousResearch/Nous-Hermes-2-Yi-34B",
|
62 |
+
"openchat/openchat-3.5-1210",
|
63 |
+
"Open-Orca/Mistral-7B-OpenOrca",
|
64 |
+
"Qwen/Qwen1.5-0.5B-Chat",
|
65 |
+
"Qwen/Qwen1.5-1.8B-Chat",
|
66 |
+
"Qwen/Qwen1.5-4B-Chat",
|
67 |
+
"Qwen/Qwen1.5-7B-Chat",
|
68 |
+
"Qwen/Qwen1.5-14B-Chat",
|
69 |
+
"Qwen/Qwen1.5-32B-Chat",
|
70 |
+
"Qwen/Qwen1.5-72B-Chat",
|
71 |
+
"Qwen/Qwen1.5-110B-Chat",
|
72 |
+
"Qwen/Qwen2-72B-Instruct",
|
73 |
+
"snorkelai/Snorkel-Mistral-PairRM-DPO",
|
74 |
+
"Snowflake/snowflake-arctic-instruct",
|
75 |
+
"togethercomputer/alpaca-7b",
|
76 |
+
"teknium/OpenHermes-2-Mistral-7B",
|
77 |
+
"teknium/OpenHermes-2p5-Mistral-7B",
|
78 |
+
"togethercomputer/Llama-2-7B-32K-Instruct",
|
79 |
+
"togethercomputer/RedPajama-INCITE-Chat-3B-v1",
|
80 |
+
"togethercomputer/RedPajama-INCITE-7B-Chat",
|
81 |
+
"togethercomputer/StripedHyena-Nous-7B",
|
82 |
+
"Undi95/ReMM-SLERP-L2-13B",
|
83 |
+
"Undi95/Toppy-M-7B",
|
84 |
+
"WizardLM/WizardLM-13B-V1.2",
|
85 |
+
"upstage/SOLAR-10.7B-Instruct-v1.0"
|
86 |
+
]
|
87 |
+
|
88 |
+
|
89 |
+
# Function to get Groq chat models
|
90 |
+
def get_groq_models():
|
91 |
+
return [
|
92 |
+
# "llama-3.1-405b-reasoning",
|
93 |
+
"llama-3.1-70b-versatile",
|
94 |
+
"mixtral-8x7b-32768",
|
95 |
+
"llama3-groq-70b-8192-tool-use-preview",
|
96 |
+
"llama-3.1-8b-instant",
|
97 |
+
"llama3-groq-8b-8192-tool-use-preview",
|
98 |
+
"llama-guard-3-8b",
|
99 |
+
"llama3-70b-8192",
|
100 |
+
"llama3-8b-8192",
|
101 |
+
"gemma-7b-it",
|
102 |
+
"gemma2-9b-it",
|
103 |
+
"whisper-large-v3"
|
104 |
+
]
|
105 |
+
|
106 |
+
# Function to get OpenAI-like models
|
107 |
+
def get_openai_like_models():
|
108 |
+
return [
|
109 |
+
"claude-3-5-sonnet",
|
110 |
+
"gpt-4-turbo-128k-france",
|
111 |
+
"gemini-1.0-pro",
|
112 |
+
"gemini-1.5-pro",
|
113 |
+
"gemini-1.5-flash",
|
114 |
+
"Llama-3-70B-Instruct",
|
115 |
+
"Mixtral-8x7B-Instruct-v0.1",
|
116 |
+
"CodeLlama-2",
|
117 |
+
"jina-embeddings-v2-base-de",
|
118 |
+
"jina-embeddings-v2-base-code",
|
119 |
+
"text-embedding-bge-m3",
|
120 |
+
"llava-v1.6-34b",
|
121 |
+
"llava-v1.6-vicuna-13b",
|
122 |
+
"gpt-35-turbo",
|
123 |
+
"text-embedding-ada-002",
|
124 |
+
"gpt-4-32k-1",
|
125 |
+
"gpt-4-32k-canada",
|
126 |
+
"gpt-4-32k-france",
|
127 |
+
"text-embedding-ada-002-france",
|
128 |
+
"mistral-large-32k-france",
|
129 |
+
"Llama-3.1-405B-Instruct-US",
|
130 |
+
"Mistral-Large-2407",
|
131 |
+
"Mistral-Nemo-2407"
|
132 |
+
]
|
133 |
+
|
134 |
+
|
135 |
+
|
136 |
+
def to_leetspeak(text):
|
137 |
+
leet_dict = {
|
138 |
+
'a': '4', 'e': '3', 'g': '6', 'i': '1', 'o': '0', 's': '5', 't': '7',
|
139 |
+
'A': '4', 'E': '3', 'G': '6', 'I': '1', 'O': '0', 'S': '5', 'T': '7'
|
140 |
+
}
|
141 |
+
return ''.join(leet_dict.get(char, char) for char in text)
|
142 |
+
|
143 |
+
def to_base64(text):
|
144 |
+
return base64.b64encode(text.encode()).decode()
|
145 |
+
|
146 |
+
def to_binary(text):
|
147 |
+
return ' '.join(format(ord(char), '08b') for char in text)
|
148 |
+
|
149 |
+
def to_emoji(text):
|
150 |
+
emoji_dict = {
|
151 |
+
'a': 'π
°', 'b': 'π
±', 'c': 'π
²', 'd': 'π
³', 'e': 'π
΄', 'f': 'π
΅', 'g': 'π
Ά', 'h': 'π
·', 'i': 'π
Έ', 'j': 'π
Ή',
|
152 |
+
'k': 'π
Ί', 'l': 'π
»', 'm': 'π
Ό', 'n': 'π
½', 'o': 'π
Ύ', 'p': 'π
Ώ', 'q': 'π', 'r': 'π', 's': 'π', 't': 'π',
|
153 |
+
'u': 'π', 'v': 'π
', 'w': 'π', 'x': 'π', 'y': 'π', 'z': 'π',
|
154 |
+
'A': 'π
°', 'B': 'π
±', 'C': 'π
²', 'D': 'π
³', 'E': 'π
΄', 'F': 'π
΅', 'G': 'π
Ά', 'H': 'π
·', 'I': 'π
Έ', 'J': 'π
Ή',
|
155 |
+
'K': 'π
Ί', 'L': 'π
»', 'M': 'π
Ό', 'N': 'π
½', 'O': 'π
Ύ', 'P': 'π
Ώ', 'Q': 'π', 'R': 'π', 'S': 'π', 'T': 'π',
|
156 |
+
'U': 'π', 'V': 'π
', 'W': 'π', 'X': 'π', 'Y': 'π', 'Z': 'π'
|
157 |
+
}
|
158 |
+
return ''.join(emoji_dict.get(char, char) for char in text)
|
159 |
+
|
160 |
+
# Initialize session state
|
161 |
+
if 'messages' not in st.session_state:
|
162 |
+
st.session_state.messages = []
|
163 |
+
|
164 |
+
if 'api_key' not in st.session_state:
|
165 |
+
st.session_state.api_key = ""
|
166 |
+
|
167 |
+
if 'selected_model' not in st.session_state:
|
168 |
+
st.session_state.selected_model = ""
|
169 |
+
|
170 |
+
if 'selected_service' not in st.session_state:
|
171 |
+
st.session_state.selected_service = ""
|
172 |
+
|
173 |
+
if 'base_url' not in st.session_state:
|
174 |
+
st.session_state.base_url = ""
|
175 |
+
|
176 |
+
# Sidebar
|
177 |
+
st.sidebar.title("Chat Settings")
|
178 |
+
|
179 |
+
|
180 |
+
# Service selection
|
181 |
+
service = st.sidebar.radio("Select a service:", ("Together AI","OpenAI-like", "Groq"))
|
182 |
+
st.session_state.selected_service = service
|
183 |
+
|
184 |
+
# Model selection based on the chosen service
|
185 |
+
if service == "OpenAI-like":
|
186 |
+
openai_like_models = get_openai_like_models()
|
187 |
+
selected_model = st.sidebar.selectbox("Select an OpenAI-like model:", openai_like_models)
|
188 |
+
base_url = st.sidebar.text_input("Enter the base URL for the OpenAI-like API:",type="password",value=os.getenv('API_BASE'))
|
189 |
+
api_key = st.sidebar.text_input("Enter your API Key:", type="password",value=os.getenv('API_KEY'))
|
190 |
+
if api_key:
|
191 |
+
st.session_state.api_key = api_key
|
192 |
+
if base_url:
|
193 |
+
st.session_state.base_url = base_url
|
194 |
+
|
195 |
+
|
196 |
+
|
197 |
+
elif service == "Groq":
|
198 |
+
groq_models = get_groq_models()
|
199 |
+
api_key = st.sidebar.text_input("Enter your API Key:", type="password",value=os.getenv('GROQ_API_KEY'))
|
200 |
+
if api_key:
|
201 |
+
st.session_state.api_key = api_key
|
202 |
+
selected_model = st.sidebar.selectbox("Select a Groq model:", groq_models)
|
203 |
+
base_url = "https://api.groq.com/openai/v1"
|
204 |
+
else: # OpenAI-like
|
205 |
+
together_models = get_together_models()
|
206 |
+
api_key = st.sidebar.text_input("Enter your API Key:", type="password",value=os.getenv('TOGETHER_API_KEY'))
|
207 |
+
if api_key:
|
208 |
+
st.session_state.api_key = api_key
|
209 |
+
selected_model = st.sidebar.selectbox("Select a Together AI model:", together_models)
|
210 |
+
base_url = "https://api.together.xyz/v1"
|
211 |
+
|
212 |
+
if selected_model:
|
213 |
+
st.session_state.selected_model = selected_model
|
214 |
+
|
215 |
+
# Main chat interface
|
216 |
+
st.title("AI Chat Application")
|
217 |
+
|
218 |
+
# Display chat messages
|
219 |
+
for message in st.session_state.messages:
|
220 |
+
with st.chat_message(message["role"]):
|
221 |
+
st.markdown(message["content"])
|
222 |
+
|
223 |
+
# User input
|
224 |
+
if prompt := st.chat_input("You:"):
|
225 |
+
if not st.session_state.api_key:
|
226 |
+
st.error("Please enter an API key.")
|
227 |
+
elif not st.session_state.selected_model:
|
228 |
+
st.error("Please select a model.")
|
229 |
+
elif service == "OpenAI-like" and not st.session_state.base_url:
|
230 |
+
st.error("Please enter the base URL for the OpenAI-like API.")
|
231 |
+
else:
|
232 |
+
# Add user message to chat history
|
233 |
+
st.session_state.messages.append({"role": "user", "content": prompt})
|
234 |
+
with st.chat_message("user"):
|
235 |
+
st.markdown(prompt)
|
236 |
+
|
237 |
+
# Generate AI response
|
238 |
+
with st.chat_message("assistant"):
|
239 |
+
message_placeholder = st.empty()
|
240 |
+
full_response = ""
|
241 |
+
|
242 |
+
try:
|
243 |
+
if service == "OpenAI-like":
|
244 |
+
client = OpenAI(
|
245 |
+
api_key=st.session_state.api_key,
|
246 |
+
base_url=st.session_state.base_url + '/v2',
|
247 |
+
)
|
248 |
+
else:
|
249 |
+
client = OpenAI(api_key=st.session_state.api_key, base_url=base_url)
|
250 |
+
|
251 |
+
for response in client.chat.completions.create(
|
252 |
+
model=st.session_state.selected_model,
|
253 |
+
messages=[
|
254 |
+
{"role": m["role"], "content": m["content"]}
|
255 |
+
for m in st.session_state.messages
|
256 |
+
],
|
257 |
+
stream=True,
|
258 |
+
max_tokens=1000,
|
259 |
+
temperature=0.7
|
260 |
+
):
|
261 |
+
full_response += (response.choices[0].delta.content or "")
|
262 |
+
message_placeholder.markdown(full_response + "β")
|
263 |
+
|
264 |
+
message_placeholder.markdown(full_response)
|
265 |
+
except Exception as e:
|
266 |
+
st.error(f"An error occurred: {str(e)}")
|
267 |
+
full_response = "I apologize, but an error occurred while generating the response."
|
268 |
+
|
269 |
+
# Add assistant response to chat history
|
270 |
+
st.session_state.messages.append({"role": "assistant", "content": full_response})
|
271 |
+
|
272 |
+
# Clear chat button
|
273 |
+
if st.sidebar.button("Clear Chat"):
|
274 |
+
st.session_state.messages = []
|
275 |
+
|
276 |
+
st.rerun()
|
277 |
+
|
278 |
+
|
279 |
+
with st.sidebar:
|
280 |
+
st.title("Text Conversion")
|
281 |
+
input_text = st.text_area("Enter text to convert:")
|
282 |
+
|
283 |
+
col1, col2 = st.columns(2)
|
284 |
+
|
285 |
+
with col1:
|
286 |
+
if st.button("To Leetspeak"):
|
287 |
+
if input_text:
|
288 |
+
converted_text = to_leetspeak(input_text)
|
289 |
+
st.text_area("Leetspeak Result:", converted_text, height=100)
|
290 |
+
st.code(converted_text, language="text")
|
291 |
+
|
292 |
+
if st.button("To Base64"):
|
293 |
+
if input_text:
|
294 |
+
converted_text = to_base64(input_text)
|
295 |
+
st.text_area("Base64 Result:", converted_text, height=100)
|
296 |
+
st.code(converted_text, language="text")
|
297 |
+
|
298 |
+
with col2:
|
299 |
+
if st.button("To Binary"):
|
300 |
+
if input_text:
|
301 |
+
converted_text = to_binary(input_text)
|
302 |
+
st.text_area("Binary Result:", converted_text, height=100)
|
303 |
+
st.code(converted_text, language="text")
|
304 |
+
|
305 |
+
if st.button("To Emoji"):
|
306 |
+
if input_text:
|
307 |
+
converted_text = to_emoji(input_text)
|
308 |
+
st.text_area("Emoji Result:", converted_text, height=100)
|
309 |
+
st.code(converted_text, language="text")
|
310 |
+
|
311 |
+
|