Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import requests | |
| import os | |
| GROQ_API_KEY = os.getenv("GROQ_API_KEY") # Add your Groq key in Hugging Face secrets | |
| # Prompt template | |
| SYSTEM_PROMPT = """ | |
| You are a Mechanical Fault Diagnosis Assistant. Users describe symptoms of mechanical systems | |
| (like motors, pumps, compressors, or gear assemblies). | |
| Based on the input, provide: | |
| - Possible Cause | |
| - Recommended Fix | |
| - Tools Needed | |
| Reply in this format: | |
| **Possible Cause:** | |
| **Recommended Fix:** | |
| **Tools Needed:** | |
| ... | |
| """ | |
| def diagnose_fault(user_input): | |
| url = "https://api.groq.com/openai/v1/chat/completions" | |
| headers = { | |
| "Authorization": f"Bearer {GROQ_API_KEY}", | |
| "Content-Type": "application/json" | |
| } | |
| payload = { | |
| "model": "llama3-8b-8192", | |
| "messages": [ | |
| {"role": "system", "content": SYSTEM_PROMPT}, | |
| {"role": "user", "content": user_input} | |
| ], | |
| "temperature": 0.5 | |
| } | |
| try: | |
| response = requests.post(url, headers=headers, json=payload) | |
| response.raise_for_status() | |
| return response.json()['choices'][0]['message']['content'] | |
| except Exception as e: | |
| return f"❌ Error: {str(e)}" | |
| iface = gr.Interface( | |
| fn=diagnose_fault, | |
| inputs=gr.Textbox(lines=4, placeholder="Describe your machine fault (e.g., gear is noisy)..."), | |
| outputs=gr.Markdown(), | |
| title="🛠️ Failure Diagnosis Bot", | |
| description="Describe a mechanical issue, and this bot will help you find the cause, fix, and required tools." | |
| ) | |
| if __name__ == "__main__": | |
| iface.launch() | |