File size: 3,380 Bytes
294266d
 
a2f1c6d
294266d
 
 
 
a2f1c6d
 
 
 
 
 
 
 
 
229b0f2
 
 
 
 
 
 
 
 
 
a2f1c6d
 
229b0f2
 
 
294266d
 
a2f1c6d
 
 
 
 
294266d
 
 
a2f1c6d
294266d
a2f1c6d
229b0f2
 
 
 
 
 
 
 
 
 
 
 
 
 
a2f1c6d
 
294266d
229b0f2
 
 
 
 
 
 
 
 
 
 
294266d
229b0f2
294266d
229b0f2
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
77
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()