Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import requests | |
| import os | |
| # Groq API key from environment variable (set in Hugging Face Secrets) | |
| GROQ_API_KEY = os.getenv("GROQ_API_KEY") | |
| GROQ_API_URL = "https://api.groq.com/openai/v1/chat/completions" | |
| MODEL = "llama3-8b-8192" | |
| def diagnose(symptoms): | |
| if not GROQ_API_KEY: | |
| return "❌ Error: GROQ_API_KEY not found. Please set it in the environment variables." | |
| headers = { | |
| "Authorization": f"Bearer {GROQ_API_KEY}", | |
| "Content-Type": "application/json" | |
| } | |
| messages = [ | |
| {"role": "system", "content": "You are an expert mechanical diagnostic assistant that helps users identify faults in mechanical systems based on described symptoms. Provide probable causes and practical remedies in clear, technical but understandable terms."}, | |
| {"role": "user", "content": symptoms} | |
| ] | |
| data = { | |
| "model": MODEL, | |
| "messages": messages, | |
| "temperature": 0.7 | |
| } | |
| response = requests.post(GROQ_API_URL, headers=headers, json=data) | |
| if response.status_code == 200: | |
| return response.json()["choices"][0]["message"]["content"] | |
| else: | |
| return f"❌ API Error: {response.status_code} - {response.text}" | |
| # Gradio UI | |
| iface = gr.Interface( | |
| fn=diagnose, | |
| inputs=gr.Textbox(lines=10, placeholder="Describe the mechanical issue or symptoms..."), | |
| outputs="text", | |
| title="🛠️ Failure Diagnostic Bot - Machine Fault Analyzer", | |
| description="Enter the symptoms of a mechanical problem, and this assistant will help diagnose the issue and suggest possible causes and remedies.", | |
| ) | |
| if __name__ == "__main__": | |
| iface.launch() | |