shivrajkarewar's picture
Update app.py
229b0f2 verified
raw
history blame
3.38 kB
import os
import requests
import gradio as gr
# 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):
# Check if question is related to materials science
keywords = [
"material", "materials", "alloy", "composite", "polymer", "ceramic",
"application", "mechanical properties", "thermal properties", "corrosion",
"creep", "fatigue", "strength", "tensile", "impact", "fracture", "modulus"
]
if not any(word in user_input.lower() for word in keywords):
return "⚠️ I am an expert in Materials Science, ask me anything about it and I will try my best to answer. Anything outside, feel free to use ChatGPT! πŸ™‚"
system_prompt = (
"You are an expert materials scientist. When a user asks about the best materials for a specific application, "
"provide the top 3 material choices. First, list the key properties required for that application. Then show a clean, "
"side-by-side comparison in markdown table format of the three materials, with the properties as rows and materials as columns. "
"Include their relevant mechanical, thermal, and chemical properties. Conclude with a brief summary of which might be best depending on the scenario."
)
body = {
"model": "llama-3.1-8b-instant",
"messages": [
{"role": "system", "content": system_prompt},
{"role": "user", "content": user_input}
]
}
response = requests.post(url, headers=headers, json=body)
if response.status_code == 200:
base_response = response.json()['choices'][0]['message']['content']
# Append popular related questions for UAE
popular_questions = (
"\n\n---\n"
"#### πŸ” Popular related questions in the UAE:\n"
"- What are the best corrosion-resistant materials for marine environments (e.g., desalination)?\n"
"- Which materials are ideal for solar panel coatings and desert heat management?\n"
"- What materials are used for aerospace structures in extreme climates?\n"
"- Best high-strength materials for construction in the Gulf region?\n"
"- What advanced materials are used in electric vehicles and batteries in the UAE?\n"
)
return base_response + popular_questions
else:
return f"Error: {response.json()}"
# Build improved layout using Gradio Blocks
with gr.Blocks(title="Materials Science Expert Chatbot") as demo:
gr.Markdown("## πŸ§ͺ Materials Science Expert\nAsk about the best materials for a specific application.")
with gr.Row():
user_input = gr.Textbox(lines=2, placeholder="e.g. Best materials for high-temperature turbine blades...", label="Ask your question")
submit_btn = gr.Button("Submit")
output = gr.Markdown()
submit_btn.click(chat_with_groq, inputs=user_input, outputs=output)
# Launch the app
if __name__ == "__main__":
demo.launch()