shivrajkarewar commited on
Commit
30b0367
·
verified ·
1 Parent(s): 2c510ed

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -59
app.py CHANGED
@@ -1,14 +1,12 @@
1
  import os
2
  import requests
3
  import gradio as gr
4
- from pymatgen.core import Structure, Lattice
5
- from pymatgen.io.cif import CifWriter
6
  import tempfile
7
- import shutil
8
 
9
  # Retrieve the API key from the environment variable
10
  groq_api_key = os.getenv("GROQ_API_KEY")
11
-
12
  if not groq_api_key:
13
  raise ValueError("GROQ_API_KEY is missing! Set it in the Hugging Face Spaces 'Secrets'.")
14
 
@@ -16,75 +14,50 @@ if not groq_api_key:
16
  url = "https://api.groq.com/openai/v1/chat/completions"
17
  headers = {"Authorization": f"Bearer {groq_api_key}"}
18
 
19
- # Function to get material recommendations from Groq API
20
- def get_material_recommendations(application):
21
  body = {
22
  "model": "llama-3.1-8b-instant",
23
- "messages": [{"role": "user", "content": f"Recommend three best materials for {application} with their key properties."}]
24
  }
25
-
26
  response = requests.post(url, headers=headers, json=body)
27
-
28
  if response.status_code == 200:
29
  return response.json()['choices'][0]['message']['content']
30
  else:
31
  return f"Error: {response.json()}"
32
 
33
- # Function to create dummy structures for visualization
34
- def create_dummy_structure(material_name):
35
- # This function creates a simple cubic structure as a placeholder
36
- lattice = Lattice.cubic(3.5)
37
- structure = Structure(
38
- lattice,
39
- ["Si"],
40
- [[0, 0, 0]]
41
- )
42
- return structure
43
-
44
- # Function to generate CIF files and visualize structures
45
- def generate_and_visualize(application):
46
- # Get material recommendations
47
- recommendations = get_material_recommendations(application)
48
-
49
- # Parse the recommendations to extract material names
50
- # For simplicity, we'll assume the material names are listed as:
51
- # 1. Material A
52
- # 2. Material B
53
- # 3. Material C
54
- lines = recommendations.split('\n')
55
- material_names = []
56
- for line in lines:
57
- if line.strip().startswith(('1.', '2.', '3.')):
58
- parts = line.split('.')
59
- if len(parts) > 1:
60
- material_name = parts[1].strip().split(' ')[0]
61
- material_names.append(material_name)
62
-
63
- # Create temporary directory to store CIF files
64
- temp_dir = tempfile.mkdtemp()
65
- cif_file_paths = []
66
- for name in material_names:
67
- structure = create_dummy_structure(name)
68
- cif_path = os.path.join(temp_dir, f"{name}.cif")
69
- writer = CifWriter(structure)
70
- writer.write_file(cif_path)
71
- cif_file_paths.append(cif_path)
72
 
73
- # Display the recommendations and provide CIF files for download
74
- output_text = recommendations
75
- download_links = [gr.File(cif_path, label=os.path.basename(cif_path)) for cif_path in cif_file_paths]
76
 
77
- return output_text, download_links
 
 
 
 
 
 
 
 
 
 
 
 
 
 
78
 
79
- # Create Gradio interface
80
  interface = gr.Interface(
81
- fn=generate_and_visualize,
82
- inputs=gr.Textbox(lines=2, placeholder="Enter application (e.g., aerospace structural component)"),
83
- outputs=[gr.Textbox(label="Material Recommendations"), gr.File(label="Download CIF Files")],
84
- title="Materials Science Expert",
85
- description="Enter a specific application to get the top 3 material recommendations with their properties and download their CIF files."
86
  )
87
 
88
- # Launch Gradio app
89
  if __name__ == "__main__":
90
  interface.launch()
 
1
  import os
2
  import requests
3
  import gradio as gr
4
+ from ase.io import read
5
+ import nglview as nv
6
  import tempfile
 
7
 
8
  # Retrieve the API key from the environment variable
9
  groq_api_key = os.getenv("GROQ_API_KEY")
 
10
  if not groq_api_key:
11
  raise ValueError("GROQ_API_KEY is missing! Set it in the Hugging Face Spaces 'Secrets'.")
12
 
 
14
  url = "https://api.groq.com/openai/v1/chat/completions"
15
  headers = {"Authorization": f"Bearer {groq_api_key}"}
16
 
17
+ # Function to interact with Groq API
18
+ def chat_with_groq(user_input):
19
  body = {
20
  "model": "llama-3.1-8b-instant",
21
+ "messages": [{"role": "user", "content": user_input}]
22
  }
 
23
  response = requests.post(url, headers=headers, json=body)
 
24
  if response.status_code == 200:
25
  return response.json()['choices'][0]['message']['content']
26
  else:
27
  return f"Error: {response.json()}"
28
 
29
+ # Function to generate and visualize CIF files
30
+ def generate_and_visualize_cifs(application):
31
+ # Get response from Groq API
32
+ response = chat_with_groq(f"Suggest 3 best materials for {application} with their properties and provide CIF data.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33
 
34
+ # Parse the response to extract materials and their CIF contents
35
+ # This is a placeholder; actual implementation depends on response format
36
+ materials = parse_response(response)
37
 
38
+ views = []
39
+ for material in materials:
40
+ cif_content = material['cif_content']
41
+ with tempfile.NamedTemporaryFile(mode='w+', suffix='.cif', delete=False) as tmp:
42
+ tmp.write(cif_content)
43
+ tmp_path = tmp.name
44
+ atoms = read(tmp_path)
45
+ view = nv.show_ase(atoms)
46
+ views.append(view)
47
+ return views
48
+
49
+ # Gradio interface
50
+ def interface_function(application):
51
+ views = generate_and_visualize_cifs(application)
52
+ return views
53
 
 
54
  interface = gr.Interface(
55
+ fn=interface_function,
56
+ inputs=gr.Textbox(lines=2, placeholder="Enter material application..."),
57
+ outputs="text",
58
+ title="Material Selector and Visualizer",
59
+ description="Enter a material application to get top 3 materials and visualize their atomic structures."
60
  )
61
 
 
62
  if __name__ == "__main__":
63
  interface.launch()