smitathkr1 commited on
Commit
2d883ce
·
verified ·
1 Parent(s): 93509a1

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +659 -0
app.py ADDED
@@ -0,0 +1,659 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from groq import Groq
2
+ import streamlit as st
3
+ from openai import OpenAI
4
+ import json
5
+ import streamlit.components.v1 as components
6
+ import requests
7
+ from youtube_transcript_api import YouTubeTranscriptApi
8
+ from youtubesearchpython import VideosSearch
9
+ from rdkit import Chem
10
+ from rdkit.Chem import Draw, AllChem
11
+ import os
12
+ import queue
13
+ import re
14
+ import tempfile
15
+ import threading
16
+ import requests
17
+ from bs4 import BeautifulSoup
18
+ from embedchain import App
19
+ from embedchain.config import BaseLlmConfig
20
+ from embedchain.helpers.callbacks import (StreamingStdOutCallbackHandlerYield,
21
+ generate)
22
+
23
+ client_groq = Groq(api_key="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")
24
+ client_openai = OpenAI(api_key="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")
25
+
26
+ link_custom_functions = [
27
+ {
28
+ 'name': 'extract_website_url',
29
+ 'description': 'Get the website url',
30
+ 'parameters': {
31
+ 'type': 'object',
32
+ 'properties': {
33
+ 'link': {'type': 'string', 'description': 'website url'},
34
+ }
35
+ }
36
+ }
37
+ ]
38
+
39
+ def embedchain_bot(db_path, api_key):
40
+ return App.from_config(
41
+ config={
42
+ "llm": {
43
+ "provider": "openai",
44
+ "config": {
45
+ "model": "gpt-3.5-turbo-1106",
46
+ "temperature": 0.5,
47
+ "max_tokens": 1000,
48
+ "top_p": 1,
49
+ "stream": True,
50
+ "api_key": api_key,
51
+ },
52
+ },
53
+ "vectordb": {
54
+ "provider": "chroma",
55
+ "config": {"collection_name": "chat-pdf", "dir": db_path, "allow_reset": True},
56
+ },
57
+ "embedder": {"provider": "openai", "config": {"api_key": api_key}},
58
+ "chunker": {"chunk_size": 2000, "chunk_overlap": 0, "length_function": "len"},
59
+ }
60
+ )
61
+
62
+
63
+ def get_db_path():
64
+ tmpdirname = tempfile.mkdtemp()
65
+ return tmpdirname
66
+
67
+
68
+ def get_ec_app(api_key):
69
+ if "app" in st.session_state:
70
+ print("Found app in session state")
71
+ app = st.session_state.app
72
+ else:
73
+ print("Creating app")
74
+ db_path = get_db_path()
75
+ app = embedchain_bot(db_path, api_key)
76
+ st.session_state.app = app
77
+ return app
78
+
79
+ # Initialize your clients with API keys
80
+ client_openai = OpenAI(api_key="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")
81
+ client_groq = Groq(api_key="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")
82
+ client_groq_one = Groq(api_key="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")
83
+
84
+ # Define your custom functions for OpenAI
85
+ scenario_custom_functions = [
86
+ {
87
+ 'name': 'extract_scenario_info',
88
+ 'description': 'Get the individual scenarios text',
89
+ 'parameters': {
90
+ 'type': 'object',
91
+ 'properties': {
92
+ 'scenario_1': {'type': 'string', 'description': 'scenario number 1 full text'},
93
+ 'scenario_2': {'type': 'string', 'description': 'scenario number 2 full text'},
94
+ 'scenario_3': {'type': 'string', 'description': 'scenario number 3 full text'},
95
+ 'scenario_4': {'type': 'string', 'description': 'scenario number 4 full text'},
96
+ }
97
+ }
98
+ }
99
+ ]
100
+
101
+ scenario_keyword_functions = [
102
+ {
103
+ 'name': 'extract_scenario_info',
104
+ 'description': 'Get the individual scenarios text',
105
+ 'parameters': {
106
+ 'type': 'object',
107
+ 'properties': {
108
+ 'keyword_1': {'type': 'string', 'description': 'keyword 1'},
109
+ 'keyword_2': {'type': 'string', 'description': 'keyword 2'},
110
+ 'keyword_3': {'type': 'string', 'description': 'keyword 3'},
111
+ 'keyword_4': {'type': 'string', 'description': 'keyword 4'},
112
+ }
113
+ }
114
+ }
115
+ ]
116
+
117
+ video_custom_functions = [
118
+ {
119
+ 'name': 'extract_video_id',
120
+ 'description': 'Get the video ID',
121
+ 'parameters': {
122
+ 'type': 'object',
123
+ 'properties': {
124
+ 'video_id': {'type': 'string', 'description': 'video ID'},
125
+ }
126
+ }
127
+ }
128
+ ]
129
+ # Initialize a string to store all transcripts
130
+ all_video_transcripts = ""
131
+
132
+ molecule_custom_functions = [
133
+ {
134
+ 'name': 'extract_molecule_info',
135
+ 'description': 'Get the molecule name',
136
+ 'parameters': {
137
+ 'type': 'object',
138
+ 'properties': {
139
+ 'molecule_name': {'type': 'string', 'description': 'name of the molecule'},
140
+ }
141
+ }
142
+ }
143
+ ]
144
+
145
+ keyword_custom_functions = [
146
+ {
147
+ 'name': 'extract_keyword_info',
148
+ 'description': 'Get the search query keyword',
149
+ 'parameters': {
150
+ 'type': 'object',
151
+ 'properties': {
152
+ 'keyword': {'type': 'string', 'description': 'keyword of teh search query'},
153
+ }
154
+ }
155
+ }
156
+ ]
157
+
158
+ # Example SMILES strings for each component - replace these with the actual values retrieved from your API calls
159
+ reactant_1_smiles = 'your_reactant_1_smiles_here'
160
+ reactant_2_smiles = 'your_reactant_2_smiles_here' # This might be an empty string if not present
161
+ reagent_3_smiles = 'your_reagent_3_smiles_here'
162
+ product_4_smiles = 'your_product_4_smiles_here'
163
+ product_5_smiles = 'your_product_5_smiles_here'
164
+ molecule_custom_functions_reaction = [
165
+ {
166
+ 'name': 'extract_molecules_info',
167
+ 'description': 'Get the name of the individual molecules',
168
+ 'parameters': {
169
+ 'type': 'object',
170
+ 'properties': {
171
+ 'reactant_1': {'type': 'string', 'description': 'reactant number 1 '},
172
+ 'reactant_2': {'type': 'string', 'description': 'reactant number 2 '},
173
+ 'reagent_3': {'type': 'string', 'description': 'reagent number 1 '},
174
+ 'product_4': {'type': 'string', 'description': 'product number 1'},
175
+ 'product_5': {'type': 'string', 'description': 'product number 2'},
176
+ }
177
+ }
178
+ }
179
+ ]
180
+
181
+ # Streamlit UI
182
+ st.title("Stereo World 🌍")
183
+ image_variable = None
184
+ # Session states initialization
185
+ if 'prompt' not in st.session_state:
186
+ st.session_state.prompt = ''
187
+ if 'selected_options' not in st.session_state:
188
+ st.session_state.selected_options = []
189
+ if 'selected_options_reaction' not in st.session_state:
190
+ st.session_state.selected_options_reaction = []
191
+
192
+
193
+ # User inputs
194
+ st.session_state.prompt = st.text_input("Enter your prompt")
195
+ st.session_state.selected_options = st.multiselect("Select options", ["fun based", "context based", "real world based", "conceptual textbook based"])
196
+ check_box = st.checkbox("Open Chem Sketcher")
197
+ with st.sidebar:
198
+ st.sidebar.title("Chat with the assistant 🤖")
199
+ # Input for search query
200
+ search_query = st.sidebar.text_input("Enter your video search query")
201
+ reaction_query = st.sidebar.text_input("Enter your reaction search query")
202
+ name_reaction = st.checkbox("I am searching a name reaction")
203
+ if name_reaction:
204
+ with st.sidebar:
205
+ #openai_access_token = st.text_input("OpenAI API Key", key="api_key", type="password")
206
+ api_key = "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"
207
+
208
+
209
+ #if st.session_state.api_key:
210
+ app = get_ec_app(api_key)
211
+
212
+ pdf_files = st.file_uploader("Upload your PDF files", accept_multiple_files=True, type="pdf")
213
+ add_pdf_files = st.session_state.get("add_pdf_files", [])
214
+ for pdf_file in pdf_files:
215
+ file_name = pdf_file.name
216
+ if file_name in add_pdf_files:
217
+ continue
218
+ try:
219
+ temp_file_name = None
220
+ with tempfile.NamedTemporaryFile(mode="wb", delete=False, prefix=file_name, suffix=".pdf") as f:
221
+ f.write(pdf_file.getvalue())
222
+ temp_file_name = f.name
223
+ if temp_file_name:
224
+ st.markdown(f"Adding {file_name} to knowledge base...")
225
+ app.add(temp_file_name, data_type="pdf_file")
226
+ st.markdown("")
227
+ add_pdf_files.append(file_name)
228
+ os.remove(temp_file_name)
229
+ st.session_state.messages.append({"role": "assistant", "content": f"Added {file_name} to knowledge base!"})
230
+ except Exception as e:
231
+ st.error(f"Error adding {file_name} to knowledge base: {e}")
232
+ st.stop()
233
+ st.session_state["add_pdf_files"] = add_pdf_files
234
+
235
+ if "messages" not in st.session_state:
236
+ st.session_state.messages = []
237
+
238
+ for message in st.session_state.messages:
239
+ with st.chat_message(message["role"]):
240
+ st.markdown(message["content"])
241
+
242
+ if prompt := st.chat_input("Ask me anything!"):
243
+ app = get_ec_app(api_key)
244
+
245
+ with st.chat_message("user"):
246
+ st.session_state.messages.append({"role": "user", "content": prompt})
247
+ st.markdown(prompt)
248
+
249
+ with st.chat_message("assistant"):
250
+ msg_placeholder = st.empty()
251
+ #msg_placeholder.markdown("Thinking...")
252
+ full_response = ""
253
+
254
+ q = queue.Queue()
255
+
256
+ def app_response(result):
257
+ llm_config = app.llm.config.as_dict()
258
+ llm_config["callbacks"] = [StreamingStdOutCallbackHandlerYield(q=q)]
259
+ config = BaseLlmConfig(**llm_config)
260
+ answer, citations = app.chat(prompt, config=config, citations=True)
261
+ result["answer"] = answer
262
+ result["citations"] = citations
263
+
264
+ results = {}
265
+ thread = threading.Thread(target=app_response, args=(results,))
266
+ thread.start()
267
+
268
+ for answer_chunk in generate(q):
269
+ full_response += answer_chunk
270
+ msg_placeholder.markdown(full_response)
271
+
272
+ thread.join()
273
+ # answer, citations = results["answer"], results["citations"]
274
+ # if citations:
275
+ # full_response += "\n\n**Sources**:\n"
276
+ # sources = []
277
+ # for i, citation in enumerate(citations):
278
+ # source = citation[1]["url"]
279
+ # pattern = re.compile(r"([^/]+)\.[^\.]+\.pdf$")
280
+ # match = pattern.search(source)
281
+ # if match:
282
+ # source = match.group(1) + ".pdf"
283
+ # sources.append(source)
284
+ # sources = list(set(sources))
285
+ # for source in sources:
286
+ # full_response += f"- {source}\n"
287
+
288
+ msg_placeholder.markdown(full_response)
289
+ print("Answer: ", full_response)
290
+ st.session_state.messages.append({"role": "assistant", "content": full_response})
291
+ if full_response:
292
+ response_functions = client_openai.chat.completions.create(
293
+ model="gpt-3.5-turbo",
294
+ messages=[{'role': 'user', 'content': full_response}],
295
+ functions=link_custom_functions,
296
+ function_call='auto'
297
+ )
298
+ data = json.loads(response_functions.choices[0].message.function_call.arguments)
299
+ website_url = data['link']
300
+ # Check if the link starts with 'en.wiki'
301
+ request = requests.get(f"https://{website_url}")
302
+ if request.status_code == 200:
303
+ # Parse the content of the request with BeautifulSoup
304
+ soup = BeautifulSoup(request.text, 'html.parser')
305
+
306
+ # Extract all text from the webpage
307
+ text = soup.get_text(separator=' ', strip=True)
308
+
309
+ # Print the extracted text
310
+ data_websute = text
311
+ chat_completion = client_groq.chat.completions.create(
312
+ messages=[
313
+ {
314
+ "role": "user",
315
+ "content": "please give complete step by step reaction along with the complete name of the molecules for the reaction, the requested reaction is : " + data_websute,
316
+ }
317
+ ],
318
+ model="mixtral-8x7b-32768",
319
+ )
320
+ reaction = chat_completion.choices[0].message.content
321
+ response_functions = client_openai.chat.completions.create(
322
+ model="gpt-3.5-turbo",
323
+ messages=[{'role': 'user', 'content': reaction}],
324
+ functions=molecule_custom_functions_reaction,
325
+ function_call='auto'
326
+ )
327
+ data = json.loads(response_functions.choices[0].message.function_call.arguments)
328
+ reactant_1 = data.get('reactant_1', '')
329
+ reactant_2 = data.get('reactant_2', '')
330
+ reagent_3 = data.get('reagent_3', '')
331
+ product_4 = data.get('product_4', '')
332
+ product_5 = data.get('product_5', '')
333
+
334
+ reactant_1_smiles = requests.get(f"https://pubchem.ncbi.nlm.nih.gov/rest/pug/compound/name/{reactant_1}/property/CanonicalSMILES/TXT").text if requests.get(f"https://pubchem.ncbi.nlm.nih.gov/rest/pug/compound/name/{reactant_1}/property/CanonicalSMILES/TXT").status_code == 200 else ''
335
+ reactant_2_smiles = requests.get(f"https://pubchem.ncbi.nlm.nih.gov/rest/pug/compound/name/{reactant_2}/property/CanonicalSMILES/TXT").text if requests.get(f"https://pubchem.ncbi.nlm.nih.gov/rest/pug/compound/name/{reactant_2}/property/CanonicalSMILES/TXT").status_code == 200 else ''
336
+ reagent_3_smiles = requests.get(f"https://pubchem.ncbi.nlm.nih.gov/rest/pug/compound/name/{reagent_3}/property/CanonicalSMILES/TXT").text if requests.get(f"https://pubchem.ncbi.nlm.nih.gov/rest/pug/compound/name/{reagent_3}/property/CanonicalSMILES/TXT").status_code == 200 else ''
337
+ product_4_smiles = requests.get(f"https://pubchem.ncbi.nlm.nih.gov/rest/pug/compound/name/{product_4}/property/CanonicalSMILES/TXT").text if requests.get(f"https://pubchem.ncbi.nlm.nih.gov/rest/pug/compound/name/{product_4}/property/CanonicalSMILES/TXT").status_code == 200 else ''
338
+ product_5_smiles = requests.get(f"https://pubchem.ncbi.nlm.nih.gov/rest/pug/compound/name/{product_5}/property/CanonicalSMILES/TXT").text if requests.get(f"https://pubchem.ncbi.nlm.nih.gov/rest/pug/compound/name/{product_5}/property/CanonicalSMILES/TXT").status_code == 200 else ''
339
+
340
+ #st.write("Reactant 1: ", reactant_1_smiles)
341
+ #st.write("Reactant 2: ", reactant_2_smiles)
342
+ #st.write("Reagent 3: ", reagent_3_smiles)
343
+ #st.write("Product 4: ", product_4_smiles)
344
+ #st.write("Product 5: ", product_5_smiles)
345
+ # Building the reaction SMILES string dynamically based on available components
346
+ # Building the reaction SMILES string
347
+ reaction_components = []
348
+
349
+ # Adding reactants
350
+ reactants = [reactant for reactant in [reactant_1_smiles, reactant_2_smiles] if reactant]
351
+ if reactants:
352
+ reaction_components.append('.'.join(reactants))
353
+ else:
354
+ reaction_components.append('')
355
+
356
+ # Adding reagents
357
+ reagents = [reagent for reagent in [reagent_3_smiles] if reagent]
358
+ if reagents:
359
+ reaction_components.append('.'.join(reagents))
360
+ else:
361
+ reaction_components.append('')
362
+
363
+ # Adding products
364
+ products = [product for product in [product_4_smiles, product_5_smiles] if product]
365
+ if products:
366
+ reaction_components.append('.'.join(products))
367
+ else:
368
+ reaction_components.append('')
369
+
370
+ reaction_smiles = '>'.join(reaction_components)
371
+ try:
372
+ # Generate the reaction from SMILES
373
+ rxn = AllChem.ReactionFromSmarts(reaction_smiles, useSmiles=True)
374
+
375
+ # Draw the reaction
376
+ d2d = Draw.MolDraw2DCairo(800, 300) # Adjust size as needed
377
+ d2d.DrawReaction(rxn)
378
+ png = d2d.GetDrawingText()
379
+
380
+ # Save the drawing to a file
381
+ with open('reaction_image.png', 'wb+') as f:
382
+ f.write(png)
383
+ image_variable = png
384
+ #st.image('reaction_image.png')
385
+
386
+ except Exception as e:
387
+ st.write(f"An error occurred: {e}")
388
+ else:
389
+ st.write("Sorry, the website is not available.")
390
+
391
+
392
+
393
+ if reaction_query:
394
+ chat_completion = client_groq.chat.completions.create(
395
+ messages=[
396
+ {
397
+ "role": "user",
398
+ "content": "please give complete step by step reaction along with the complete name of the molecules for the reaction, the requested reaction is : " + reaction_query,
399
+ }
400
+ ],
401
+ model="mixtral-8x7b-32768",
402
+ )
403
+ reaction = chat_completion.choices[0].message.content
404
+ response_functions = client_openai.chat.completions.create(
405
+ model="gpt-3.5-turbo",
406
+ messages=[{'role': 'user', 'content': reaction}],
407
+ functions=molecule_custom_functions_reaction,
408
+ function_call='auto'
409
+ )
410
+ data = json.loads(response_functions.choices[0].message.function_call.arguments)
411
+ reactant_1 = data.get('reactant_1', '')
412
+ reactant_2 = data.get('reactant_2', '')
413
+ reagent_3 = data.get('reagent_3', '')
414
+ product_4 = data.get('product_4', '')
415
+ product_5 = data.get('product_5', '')
416
+
417
+ reactant_1_smiles = requests.get(f"https://pubchem.ncbi.nlm.nih.gov/rest/pug/compound/name/{reactant_1}/property/CanonicalSMILES/TXT").text if requests.get(f"https://pubchem.ncbi.nlm.nih.gov/rest/pug/compound/name/{reactant_1}/property/CanonicalSMILES/TXT").status_code == 200 else ''
418
+ reactant_2_smiles = requests.get(f"https://pubchem.ncbi.nlm.nih.gov/rest/pug/compound/name/{reactant_2}/property/CanonicalSMILES/TXT").text if requests.get(f"https://pubchem.ncbi.nlm.nih.gov/rest/pug/compound/name/{reactant_2}/property/CanonicalSMILES/TXT").status_code == 200 else ''
419
+ reagent_3_smiles = requests.get(f"https://pubchem.ncbi.nlm.nih.gov/rest/pug/compound/name/{reagent_3}/property/CanonicalSMILES/TXT").text if requests.get(f"https://pubchem.ncbi.nlm.nih.gov/rest/pug/compound/name/{reagent_3}/property/CanonicalSMILES/TXT").status_code == 200 else ''
420
+ product_4_smiles = requests.get(f"https://pubchem.ncbi.nlm.nih.gov/rest/pug/compound/name/{product_4}/property/CanonicalSMILES/TXT").text if requests.get(f"https://pubchem.ncbi.nlm.nih.gov/rest/pug/compound/name/{product_4}/property/CanonicalSMILES/TXT").status_code == 200 else ''
421
+ product_5_smiles = requests.get(f"https://pubchem.ncbi.nlm.nih.gov/rest/pug/compound/name/{product_5}/property/CanonicalSMILES/TXT").text if requests.get(f"https://pubchem.ncbi.nlm.nih.gov/rest/pug/compound/name/{product_5}/property/CanonicalSMILES/TXT").status_code == 200 else ''
422
+
423
+ #st.write("Reactant 1: ", reactant_1_smiles)
424
+ #st.write("Reactant 2: ", reactant_2_smiles)
425
+ #st.write("Reagent 3: ", reagent_3_smiles)
426
+ #st.write("Product 4: ", product_4_smiles)
427
+ #st.write("Product 5: ", product_5_smiles)
428
+ # Building the reaction SMILES string dynamically based on available components
429
+ # Building the reaction SMILES string
430
+ reaction_components = []
431
+
432
+ # Adding reactants
433
+ reactants = [reactant for reactant in [reactant_1_smiles, reactant_2_smiles] if reactant]
434
+ if reactants:
435
+ reaction_components.append('.'.join(reactants))
436
+ else:
437
+ reaction_components.append('')
438
+
439
+ # Adding reagents
440
+ reagents = [reagent for reagent in [reagent_3_smiles] if reagent]
441
+ if reagents:
442
+ reaction_components.append('.'.join(reagents))
443
+ else:
444
+ reaction_components.append('')
445
+
446
+ # Adding products
447
+ products = [product for product in [product_4_smiles, product_5_smiles] if product]
448
+ if products:
449
+ reaction_components.append('.'.join(products))
450
+ else:
451
+ reaction_components.append('')
452
+
453
+ reaction_smiles = '>'.join(reaction_components)
454
+ try:
455
+ # Generate the reaction from SMILES
456
+ rxn = AllChem.ReactionFromSmarts(reaction_smiles, useSmiles=True)
457
+
458
+ # Draw the reaction
459
+ d2d = Draw.MolDraw2DCairo(800, 300) # Adjust size as needed
460
+ d2d.DrawReaction(rxn)
461
+ png = d2d.GetDrawingText()
462
+
463
+ # Save the drawing to a file
464
+ with open('reaction_image.png', 'wb+') as f:
465
+ f.write(png)
466
+ image_variable = png
467
+ #st.image('reaction_image.png')
468
+
469
+
470
+ except Exception as e:
471
+ st.write(f"An error occurred: {e}")
472
+ if search_query:
473
+ video_search = client_groq.chat.completions.create(
474
+ messages=[
475
+ {
476
+ "role": "user",
477
+ "content": "please correct the spelling and write teh precise one search keyword for and only give teh keyword, only 1 and nothing else other that that :" + search_query,
478
+ }
479
+ ],
480
+ model="mixtral-8x7b-32768",
481
+ )
482
+ search_query = video_search.choices[0].message.content
483
+ response_functions = client_openai.chat.completions.create(
484
+ model="gpt-3.5-turbo",
485
+ messages=[{'role': 'user', 'content': search_query}],
486
+ functions=keyword_custom_functions,
487
+ function_call='auto'
488
+ )
489
+ data = json.loads(response_functions.choices[0].message.function_call.arguments)
490
+ keyword = data['keyword']
491
+ st.sidebar.write(keyword)
492
+ # Perform the search
493
+ videosSearch = VideosSearch(search_query, limit=3)
494
+ video_one = VideosSearch(search_query, limit=1)
495
+ for video in video_one.result()['result']:
496
+ video_one_id = video['id']
497
+
498
+ for video in videosSearch.result()['result']:
499
+ video_id = video['id'] # Extract video ID
500
+
501
+ # Display the video thumbnail
502
+ #st.image(video['thumbnails'][0]['url'])
503
+
504
+ # Display the video title
505
+ #st.write(f"**{video['title']}**")
506
+
507
+ try:
508
+ # Fetch the transcript for the video ID
509
+ transcript_list = YouTubeTranscriptApi.get_transcript(video_id, languages=['en'])
510
+
511
+ # Concatenating all text from the transcript
512
+ transcript_text = "\n".join([t['text'] for t in transcript_list])
513
+
514
+ # Concatenate the transcript to the all_video_transcripts variable
515
+ all_video_transcripts += f"\n---\nTranscript for Video ID {video_id}:\n{transcript_text}\n---\n"
516
+
517
+ except Exception as e:
518
+ error_message = "Transcript not available or error in fetching transcript."
519
+ # Concatenate the error message to the all_video_transcripts variable
520
+ all_video_transcripts += f"\n---\nTranscript for Video ID {video_id}:\n{error_message}\n---\n"
521
+
522
+ # At this point, all_video_transcripts contains transcripts for all videos concatenated as a single string.
523
+ # You can display it or process it as needed.
524
+ # Here's an example of displaying the combined transcripts:
525
+ video_id = ""
526
+ if all_video_transcripts:
527
+ #st.text_area("All Video Transcripts", all_video_transcripts, height=300)
528
+ video_compression = client_groq_one.chat.completions.create(
529
+ messages=[
530
+ {
531
+ "role": "user",
532
+ "content": "write a one sentence summary for the the given videos and always preserve and give me the vido_id always " + all_video_transcripts,
533
+ }
534
+ ],
535
+ model="mixtral-8x7b-32768",
536
+ )
537
+ compressed_transcripts = video_compression.choices[0].message.content
538
+
539
+ chat_completion = client_groq.chat.completions.create(
540
+ messages=[
541
+ {
542
+ "role": "user",
543
+ "content": "give me the best video with maximum content and the best keywords from the transcript and always preserve and give me teh vido_id always " + compressed_transcripts,
544
+ }
545
+ ],
546
+ model="mixtral-8x7b-32768",
547
+ )
548
+ #st.write(chat_completion.choices[0].message.content)
549
+ video_id_fetch = chat_completion.choices[0].message.content
550
+ #st.write(video_id_fetch)
551
+ response_functions = client_openai.chat.completions.create(
552
+ model="gpt-3.5-turbo",
553
+ messages=[{'role': 'user', 'content': video_id_fetch}],
554
+ functions=video_custom_functions,
555
+ function_call='auto'
556
+ )
557
+ data = json.loads(response_functions.choices[0].message.function_call.arguments)
558
+ video_id = data['video_id']
559
+ st.video(f"https://www.youtube.com/watch?v={video_id}")
560
+
561
+ messages = st.container(height=630)
562
+ if image_variable:
563
+ messages.chat_message("assistant").write(f"When you react {reactant_1} with {reactant_2} using {reagent_3}, you get {product_4} and {product_5}" + " here is the reaction in 2D bond representation:")
564
+ messages.image(image_variable)
565
+ if check_box:
566
+ messages.chat_message("assistant").write("Here is the Chem Sketcher for you to draw the molecule:")
567
+ with messages.chat_message("assistant"):
568
+ components.iframe("https://marvinjs.chemicalize.com/v1/fcc0cc8570204c48a6447859c71cf611/editor.html?frameId=2cd5fd97-f496-4b6f-8cbc-417acc66684f&origin=https%3A%2F%2Fwww.rcsb.org", height=600)
569
+ prompt_sidebar = st.chat_input("Say something")
570
+ if prompt_sidebar:
571
+ messages.chat_message("user").write(prompt_sidebar)
572
+ sidebar_chat = client_groq.chat.completions.create(
573
+ messages=[
574
+ {"role": "user", "content": prompt_sidebar},
575
+ ],
576
+ model="mixtral-8x7b-32768",
577
+ )
578
+ response_functions = client_openai.chat.completions.create(
579
+ model="gpt-3.5-turbo",
580
+ messages=[{'role': 'user', 'content': prompt_sidebar}],
581
+ functions=molecule_custom_functions,
582
+ function_call='auto'
583
+ )
584
+ data = json.loads(response_functions.choices[0].message.function_call.arguments)
585
+ molecule_name = data['molecule_name']
586
+ if molecule_name:
587
+ response = requests.get(f"https://pubchem.ncbi.nlm.nih.gov/rest/pug/compound/name/{molecule_name}/cids/TXT")
588
+ cid = response.text
589
+ with messages.chat_message("assistant"):
590
+ st.write(f"Here is the molecule {molecule_name} in 3D you can interact with it too 😉:")
591
+ components.iframe(f"https://embed.molview.org/v1/?mode=balls&cid={cid}")
592
+ messages.chat_message("assistant").write(sidebar_chat.choices[0].message.content)
593
+
594
+ if st.session_state.prompt:
595
+ prompt = st.session_state.prompt
596
+ selected_options = " ".join(st.session_state.selected_options)
597
+ messages = [
598
+ {"role": "user", "content": f"create a {selected_options} scenarios based task question for learning stereochemistry, create 4 scenarios each time and number them: {prompt}"},
599
+ ]
600
+ chat_completion = client_groq.chat.completions.create(
601
+ messages=messages,
602
+ model="mixtral-8x7b-32768",
603
+ )
604
+ response = chat_completion.choices[0].message.content
605
+
606
+ if response:
607
+ response_functions = client_openai.chat.completions.create(
608
+ model="gpt-3.5-turbo",
609
+ messages=[{'role': 'user', 'content': response}],
610
+ functions=scenario_custom_functions,
611
+ function_call='auto'
612
+ )
613
+ data = json.loads(response_functions.choices[0].message.function_call.arguments)
614
+
615
+ # Tabs for scenarios
616
+ scenario_tabs = ['Scenario 1', 'Scenario 2', 'Scenario 3', 'Scenario 4']
617
+ tabs = st.tabs(scenario_tabs)
618
+ for i, tab in enumerate(tabs):
619
+ with tab:
620
+ st.header(scenario_tabs[i])
621
+ scenario_text = data[f'scenario_{i+1}']
622
+ st.write(scenario_text)
623
+ chat_completion_subquestions = client_groq.chat.completions.create(
624
+ messages=[
625
+ {
626
+ "role": "user",
627
+ "content": "subdivide this scenario into three subquestions and only give the questions. The scenario is: " + scenario_text,
628
+ }
629
+ ],
630
+ model="mixtral-8x7b-32768",
631
+ )
632
+ scenario_generated = chat_completion_subquestions.choices[0].message.content
633
+ st.write(scenario_generated)
634
+ chat_completion_hint = client_groq.chat.completions.create(
635
+ messages=[
636
+ {
637
+ "role": "user",
638
+ "content": "give a sample ideal step-by-step format to attempt to answer this scenario question as a hint. Scenario: " + scenario_text,
639
+ }
640
+ ],
641
+ model="mixtral-8x7b-32768",
642
+ )
643
+ st.text_area("Enter your answer here", key=f'answer_{i}')
644
+
645
+ with st.expander("See hint for answering the question" + str(i+1) + "😀"):
646
+ st.write(chat_completion_hint.choices[0].message.content)
647
+ # Upload PDF button
648
+ uploaded_file = st.file_uploader("Upload your answer (PDF)", type="pdf", key=f"pdf_uploader_{i}")
649
+ if uploaded_file is not None:
650
+ st.success("File uploaded successfully!")
651
+
652
+
653
+ col1, col2 = st.columns(2)
654
+ with col1:
655
+ with st.expander("See explanation 3D"):
656
+ components.iframe("https://embed.molview.org/v1/?mode=balls&cid=124527813")
657
+ with col2:
658
+ with st.expander("See explanation 2D"):
659
+ components.iframe("https://marvinjs.chemicalize.com/v1/fcc0cc8570204c48a6447859c71cf611/editor.html?frameId=2cd5fd97-f496-4b6f-8cbc-417acc66684f&origin=https%3A%2F%2Fwww.rcsb.org")