Spaces:
Sleeping
Sleeping
File size: 2,542 Bytes
dbbc9f9 0b7abd6 30b0367 9a08316 dbbc9f9 30b0367 dbbc9f9 30b0367 dbbc9f9 9a08316 6b77134 30b0367 9a08316 30b0367 9a08316 30b0367 9a08316 dbbc9f9 30b0367 0b7abd6 dbbc9f9 |
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 |
import os
import requests
import gradio as gr
from ase.io import read
import nglview as nv
import tempfile
# Retrieve the API key from the environment variable
groq_api_key = os.getenv("GROQ_API_KEY")
if not groq_api_key:
raise ValueError("GROQ_API_KEY is missing! Set it in the Hugging Face Spaces 'Secrets'.")
# Define the API endpoint and headers
url = "https://api.groq.com/openai/v1/chat/completions"
headers = {"Authorization": f"Bearer {groq_api_key}"}
# Function to interact with Groq API
def chat_with_groq(user_input):
body = {
"model": "llama-3.1-8b-instant",
"messages": [{"role": "user", "content": user_input}]
}
response = requests.post(url, headers=headers, json=body)
if response.status_code == 200:
return response.json()['choices'][0]['message']['content']
else:
return f"Error: {response.json()}"
def parse_response(response):
# Dummy parser - replace this with a proper LLM response parser as needed
lines = response.split('\n')
materials = []
for line in lines:
if any(keyword in line.lower() for keyword in ["1.", "2.", "3."]):
name = line.split(".")[1].split("-")[0].strip()
materials.append(name)
return materials
# Function to generate and visualize CIF files
def generate_and_visualize_cifs(application):
# Get response from Groq API
response = chat_with_groq(f"Suggest 3 best materials for {application} with their properties and provide CIF data.")
# Parse the response to extract materials and their CIF contents
# This is a placeholder; actual implementation depends on response format
materials = parse_response(response)
views = []
for material in materials:
cif_content = material['cif_content']
with tempfile.NamedTemporaryFile(mode='w+', suffix='.cif', delete=False) as tmp:
tmp.write(cif_content)
tmp_path = tmp.name
atoms = read(tmp_path)
view = nv.show_ase(atoms)
views.append(view)
return views
# Gradio interface
def interface_function(application):
views = generate_and_visualize_cifs(application)
return views
interface = gr.Interface(
fn=interface_function,
inputs=gr.Textbox(lines=2, placeholder="Enter material application..."),
outputs="text",
title="Material Selector and Visualizer",
description="Enter a material application to get top 3 materials and visualize their atomic structures."
)
if __name__ == "__main__":
interface.launch()
|