File size: 3,476 Bytes
1822049
affa4a7
 
 
 
1822049
 
affa4a7
de9df1d
affa4a7
1822049
affa4a7
 
 
1822049
affa4a7
 
 
 
 
 
 
 
 
 
 
 
 
1822049
 
 
 
 
 
affa4a7
 
 
 
 
 
1822049
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
de9df1d
 
1822049
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import gradio as gr
import os
from groq import Groq

# --- API Setup ---
# Hugging Face Spaces reads secrets as Environment Variables
api_key = os.getenv("EngGloss")

# Check for key availability
if not api_key:
    raise ValueError("API Key not found! Please go to your Space 'Settings' > 'Secrets' and create a secret named 'EngGloss' containing your Groq API key.")

client = Groq(api_key=api_key)

# --- The System Prompt ---
system_prompt = """
You are EngGloss, an expert AI engineering tutor. Your goal is to explain technical engineering terms to students and beginners in a way that is structured, strictly factual, yet easy to understand.

When given a term, you must output the response in the following Markdown format:

### πŸ“– Term: <The Term>

**πŸ’‘ Simple Explanation:**
<A concise, jargon-free definition (2-3 sentences max). Imagine explaining this to a first-year student.>

**🍎 Real-World Analogy:**
<An analogy using everyday objects (like water, cars, cooking, or sports) to explain the concept.>

**βš™οΈ Real-World Engineering Applications:**
Provide 3 distinct examples of how this term is applied in different engineering fields. Use this bullet format:
* **[Field Name]:** Specific explanation of how the term is used here. (e.g., "Civil Engineering: Stress and Strain are calculated to ensure bridges can withstand the weight of traffic without collapsing.")
* **[Field Name]:** Specific explanation of how the term is used here.
* **[Field Name]:** Specific explanation of how the term is used here.

**βž— Typical Formula:**
<Display the core formula using LaTeX formatting (e.g., $F=ma$). If no formula exists, write "No standard formula." Brief bullet points explaining the variables.>

Constraint: If the user input is NOT an engineering or scientific term, politely refuse and ask for a valid technical term.
"""

# --- Core Logic Function ---
def explain_term(term):
    if not term.strip():
        return "⚠️ Please enter an engineering term."
    
    try:
        chat_completion = client.chat.completions.create(
            messages=[
                {
                    "role": "system",
                    "content": system_prompt,
                },
                {
                    "role": "user",
                    "content": term,
                }
            ],
            # UPDATED MODEL NAME HERE
            model="llama-3.1-8b-instant", 
            temperature=0.5,
            max_tokens=600,
        )
        return chat_completion.choices[0].message.content

    except Exception as e:
        return f"❌ Error: {str(e)}"

# --- Gradio UI ---
with gr.Blocks(theme=gr.themes.Soft()) as demo:
    gr.Markdown("# βš™οΈ EngGloss: AI Engineering Tutor")
    gr.Markdown("Enter an engineering term below to get a structured, beginner-friendly explanation.")
    
    with gr.Row():
        input_text = gr.Textbox(
            label="Enter Engineering Term", 
            placeholder="e.g. Reynolds Number, Torque, Ohm's Law",
            lines=1
        )
    
    submit_btn = gr.Button("Explain Term", variant="primary")
    
    output_markdown = gr.Markdown(label="Explanation")

    # Connect the button to the function
    submit_btn.click(fn=explain_term, inputs=input_text, outputs=output_markdown)
    # Allow pressing "Enter" to submit as well
    input_text.submit(fn=explain_term, inputs=input_text, outputs=output_markdown)

# Launch the app
if __name__ == "__main__":
    demo.launch()