CoolFace
Apppublic

MuqeemAlvi/Basic_Engineering_Terminology

sourceHugging Facemitupdated 1y agoView on Hugging Face
0likes
app.py58 linesDownload Raw Back to root
1import os2import gradio as gr3from groq import Groq4 5# Get API key from Hugging Face Secrets (set via Settings > Secrets)6api_key = os.getenv("GROQ_API_KEY")7if not api_key:8    raise ValueError("โŒ GROQ_API_KEY not found. Please set it in your Hugging Face Space secrets.")9 10# Initialize Groq client11client = Groq(api_key=api_key)12model = "llama3-8b-8192"13 14# Build prompt15def build_prompt(term):16    return f"""17You are an expert AI tutor called EngGloss. Your job is to explain engineering terms to beginners and students in a very clear and structured format.18 19Given a technical engineering term, return your response in the following format:20 21๐Ÿ” Term: {term}22 23๐Ÿ“˜ Simple Explanation: Provide a short, jargon-free explanation suitable for someone with no technical background.24 25๐Ÿง  Real-World Analogy: Use a relatable real-life analogy to explain the concept in simple terms.26 27๐Ÿ“ Typical Formula: If the term has a commonly used formula, write it out and explain what each variable means. If there is no standard formula, say "No standard formula."28 29๐Ÿ—๏ธ Engineering Applications: List 2โ€“3 engineering fields where this term is commonly applied.30 31Be concise, clear, and beginner-friendly. Avoid technical language unless absolutely necessary, and always explain any technical words you use.32"""33 34# Define the function that handles the translation35def explain_term(term):36    try:37        prompt = build_prompt(term)38        response = client.chat.completions.create(39            model=model,40            messages=[{"role": "user", "content": prompt}]41        )42        return response.choices[0].message.content43    except Exception as e:44        return f"โŒ Error: {str(e)}"45 46# Build Gradio UI47with gr.Blocks() as demo:48    gr.Markdown("## ๐Ÿง  EngGloss: Engineering Term Explainer")49    gr.Markdown("Enter any engineering term and get a clear, structured explanation suitable for beginners.")50 51    term_input = gr.Textbox(label="๐Ÿ” Engineering Term", placeholder="e.g., Torque, Shear Stress, Entropy", lines=1)52    explain_button = gr.Button("๐Ÿ“˜ Explain")53    output = gr.Textbox(label="๐Ÿ“‹ Output", lines=20)54 55    explain_button.click(fn=explain_term, inputs=term_input, outputs=output)56 57# Run the app58demo.launch()