File size: 2,345 Bytes
9eeb9f9
 
 
 
 
 
 
 
 
 
 
8b5e517
 
 
 
 
 
e103419
 
8b5e517
 
a0708ab
8b5e517
 
 
 
a0708ab
8b5e517
e103419
a0708ab
 
 
 
 
 
8b5e517
a0708ab
8b5e517
 
 
 
a0708ab
8b5e517
9eeb9f9
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import streamlit as st
import requests

# Set your Groq API key (replace with your actual key or use an environment variable)
GROQ_API_KEY = os.getenv("GROQ_API_KEY", "YOUR_GROQ_API_KEY")

# Groq API endpoint (compatible with OpenAI format)
GROQ_API_URL = "https://api.groq.com/openai/v1/chat/completions"

# Function to generate explanation
def generate_explanation(engineering_term: str) -> str:
    prompt = (
        "You are a friendly and knowledgeable engineering professor. Explain the engineering term provided below in a clear and accessible manner. "
        "Your response should be structured into four sections:\n\n"
        "1. **Definition:** Provide a concise definition.\n"
        "2. **Background/Context:** Explain the history or context behind the term.\n"
        "3. **Application/Significance:** Describe how this concept is used in **a real-world engineering task or product** (e.g., a machine, device, or construction process).\n"
        "4. **Example:** Give a clear, everyday analogy or practical situation a user can relate to (e.g., how it works in a car, smartphone, or bridge).\n\n"
        f"Term: {engineering_term}"
    )

    headers = {
        "Content-Type": "application/json",
        "Authorization": f"Bearer {GROQ_API_KEY}",
    }

    payload = {
        "model": "llama3-8b-8192",  # Replace with your actual Groq-supported model
        "messages": [
            {"role": "system", "content": "You are a helpful and friendly engineering professor."},
            {"role": "user", "content": prompt}
        ],
        "temperature": 0.7,
        "max_tokens": 500
    }

    try:
        response = requests.post(GROQ_API_URL, headers=headers, json=payload)
        response.raise_for_status()
        result = response.json()
        return result['choices'][0]['message']['content']
    except Exception as e:
        return f"โŒ An error occurred while fetching explanation:\n\n{e}"

# Streamlit UI
st.set_page_config(page_title="Engineering Term Explainer", page_icon="๐Ÿ”")
st.title("๐Ÿ”ง Engineering Term Explainer")
st.write("Enter an engineering term to get a structured, friendly explanation:")

term = st.text_input("Engineering Term")

if term:
    with st.spinner("Generating explanation..."):
        explanation = generate_explanation(term)
    st.markdown(explanation)