Rezuwan commited on
Commit
bb38a61
·
verified ·
1 Parent(s): 8a22f44

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +43 -19
  2. requirements.txt +0 -1
app.py CHANGED
@@ -1,14 +1,28 @@
 
1
  import gradio as gr
2
  import pubchempy as pcp
3
 
 
 
 
 
 
 
4
  def get_chemical_info(chemical_name):
 
 
 
5
  try:
6
- compound = pcp.get_compounds(chemical_name, 'name')[0]
 
 
 
 
7
 
8
  output = []
9
  output.append(f"IUPAC Name: {compound.iupac_name}")
10
- output.append(f"Common Name: {compound.synonyms[0]}")
11
- output.append(f"Synonyms: {', '.join(compound.synonyms[:4])}")
12
  output.append(f"Formula: {compound.molecular_formula}")
13
  output.append(f"Molecular Weight: {compound.molecular_weight}")
14
  output.append(f"Exact Molecular Weight: {compound.exact_mass}")
@@ -31,7 +45,9 @@ def get_chemical_info(chemical_name):
31
  final_text = "\n".join(output)
32
 
33
  # Save to a file
34
- file_path = f"/tmp/{chemical_name.replace(' ', '_')}_info.txt"
 
 
35
  with open(file_path, "w") as f:
36
  f.write(final_text)
37
 
@@ -39,26 +55,32 @@ def get_chemical_info(chemical_name):
39
 
40
  except IndexError:
41
  return f"No information found for '{chemical_name}'. Please try a more precise name.", None
 
 
 
 
 
 
42
 
43
 
44
  # Gradio UI
45
  with gr.Blocks(title="ChemQuery: Learn Molecules Easily") as demo:
46
  gr.Markdown("## ChemQuery: Learn Molecules Easily")
47
  gr.Markdown(
48
- """
49
- Enter a chemical name to explore its molecular formula, synonyms, atomic structure, and bonding details.
50
- ChemQuery is Powered by PubChem & PubChemPy — designed to make chemistry learning simple and fun for students, teachers, and curious minds alike.
51
-
52
- If you find this initiative helpful, please:
53
- - ❤️ Give it a like
54
- - 🔁 Share it with your friends, classmates, or educators
55
- - ⭐ Star the GitHub repo: [ChemQuery](https://github.com/RezuwanHassan262/ChemQuery/tree/main)
56
-
57
- > _“ChemQuery is a free and open-source chemistry learning tool built as part of an educational initiative to support students worldwide.”_
58
-
59
- Let's spread the love for science and open knowledge! 🌍🧪✨
60
-
61
- """
62
  )
63
 
64
  with gr.Row():
@@ -81,5 +103,7 @@ with gr.Blocks(title="ChemQuery: Learn Molecules Easily") as demo:
81
  download_file = gr.File(label="Download as TXT")
82
 
83
  submit_btn.click(get_chemical_info, inputs=chemical_input, outputs=[output_text, download_file])
 
84
 
85
- demo.launch()
 
 
1
+ import socket
2
  import gradio as gr
3
  import pubchempy as pcp
4
 
5
+ # PubChemPy has no built-in request timeout, so a slow/unresponsive PubChem
6
+ # API call can hang a worker indefinitely. A global socket timeout prevents
7
+ # that from ever blocking the app.
8
+ socket.setdefaulttimeout(15)
9
+
10
+
11
  def get_chemical_info(chemical_name):
12
+ if not chemical_name or not chemical_name.strip():
13
+ return "Please enter a chemical name.", None
14
+
15
  try:
16
+ compounds = pcp.get_compounds(chemical_name, 'name')
17
+ if not compounds:
18
+ return f"No information found for '{chemical_name}'. Please try a more precise name.", None
19
+
20
+ compound = compounds[0]
21
 
22
  output = []
23
  output.append(f"IUPAC Name: {compound.iupac_name}")
24
+ output.append(f"Common Name: {compound.synonyms[0] if compound.synonyms else 'N/A'}")
25
+ output.append(f"Synonyms: {', '.join(compound.synonyms[:4]) if compound.synonyms else 'N/A'}")
26
  output.append(f"Formula: {compound.molecular_formula}")
27
  output.append(f"Molecular Weight: {compound.molecular_weight}")
28
  output.append(f"Exact Molecular Weight: {compound.exact_mass}")
 
45
  final_text = "\n".join(output)
46
 
47
  # Save to a file
48
+ safe_name = "".join(c for c in chemical_name if c.isalnum() or c in (" ", "_", "-")).strip()
49
+ safe_name = safe_name.replace(" ", "_") or "compound"
50
+ file_path = f"/tmp/{safe_name}_info.txt"
51
  with open(file_path, "w") as f:
52
  f.write(final_text)
53
 
 
55
 
56
  except IndexError:
57
  return f"No information found for '{chemical_name}'. Please try a more precise name.", None
58
+ except (socket.timeout, TimeoutError):
59
+ return "PubChem took too long to respond. Please try again in a moment.", None
60
+ except Exception as e:
61
+ # Catch-all so an unexpected PubChem/network error never crashes the
62
+ # request handler or shows a raw traceback to the user.
63
+ return f"Something went wrong while looking up '{chemical_name}': {e}", None
64
 
65
 
66
  # Gradio UI
67
  with gr.Blocks(title="ChemQuery: Learn Molecules Easily") as demo:
68
  gr.Markdown("## ChemQuery: Learn Molecules Easily")
69
  gr.Markdown(
70
+ """
71
+ Enter a chemical name to explore its molecular formula, synonyms, atomic structure, and bonding details.
72
+ ChemQuery is Powered by PubChem & PubChemPy — designed to make chemistry learning simple and fun for students, teachers, and curious minds alike.
73
+
74
+ If you find this initiative helpful, please:
75
+ - ❤️ Give it a like
76
+ - 🔁 Share it with your friends, classmates, or educators
77
+ - ⭐ Star the GitHub repo: [ChemQuery](https://github.com/RezuwanHassan262/ChemQuery/tree/main)
78
+
79
+ > _“ChemQuery is a free and open-source chemistry learning tool built as part of an educational initiative to support students worldwide.”_
80
+
81
+ Let's spread the love for science and open knowledge! 🌍🧪✨
82
+
83
+ """
84
  )
85
 
86
  with gr.Row():
 
103
  download_file = gr.File(label="Download as TXT")
104
 
105
  submit_btn.click(get_chemical_info, inputs=chemical_input, outputs=[output_text, download_file])
106
+ chemical_input.submit(get_chemical_info, inputs=chemical_input, outputs=[output_text, download_file])
107
 
108
+ if __name__ == "__main__":
109
+ demo.launch()
requirements.txt CHANGED
@@ -1,2 +1 @@
1
  pubchempy==1.0.4
2
- gradio==4.28.3
 
1
  pubchempy==1.0.4