Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,61 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import requests
|
3 |
+
import os
|
4 |
+
|
5 |
+
# Load Groq API key from environment variable
|
6 |
+
GROQ_API_KEY = os.getenv("GROQ_API_KEY")
|
7 |
+
|
8 |
+
# Choose your Groq model
|
9 |
+
MODEL_NAME = "mixtral-8x7b-32768" # or "llama3-8b-8192", etc.
|
10 |
+
|
11 |
+
# Set up chat history
|
12 |
+
if "messages" not in st.session_state:
|
13 |
+
st.session_state.messages = []
|
14 |
+
|
15 |
+
# Title and description
|
16 |
+
st.title("🔧 Failure Diagnosis Bot")
|
17 |
+
st.markdown("A chatbot that helps you identify mechanical issues based on symptoms and suggests fixes and tools.")
|
18 |
+
|
19 |
+
# Input box
|
20 |
+
user_input = st.text_input("Describe your machine's problem:", key="user_input")
|
21 |
+
|
22 |
+
# Submit button
|
23 |
+
if st.button("Diagnose"):
|
24 |
+
if user_input:
|
25 |
+
st.session_state.messages.append({"role": "user", "content": user_input})
|
26 |
+
|
27 |
+
# Create the chat prompt
|
28 |
+
full_prompt = [
|
29 |
+
{"role": "system", "content": (
|
30 |
+
"You are an expert mechanical engineer bot that helps diagnose mechanical failures. "
|
31 |
+
"Given a user's input about machine problems (like grinding noise, overheating, etc.), "
|
32 |
+
"respond with the most likely cause, recommended fix, and tools needed. "
|
33 |
+
"Keep answers clear, concise, and technically accurate."
|
34 |
+
)},
|
35 |
+
*st.session_state.messages
|
36 |
+
]
|
37 |
+
|
38 |
+
# Send request to Groq API
|
39 |
+
response = requests.post(
|
40 |
+
"https://api.groq.com/openai/v1/chat/completions",
|
41 |
+
headers={
|
42 |
+
"Authorization": f"Bearer {GROQ_API_KEY}",
|
43 |
+
"Content-Type": "application/json"
|
44 |
+
},
|
45 |
+
json={
|
46 |
+
"model": MODEL_NAME,
|
47 |
+
"messages": full_prompt,
|
48 |
+
"temperature": 0.7
|
49 |
+
}
|
50 |
+
)
|
51 |
+
|
52 |
+
if response.status_code == 200:
|
53 |
+
reply = response.json()["choices"][0]["message"]["content"]
|
54 |
+
st.session_state.messages.append({"role": "assistant", "content": reply})
|
55 |
+
else:
|
56 |
+
st.error("Error contacting Groq API.")
|
57 |
+
st.json(response.json())
|
58 |
+
|
59 |
+
# Display chat history
|
60 |
+
for msg in st.session_state.messages:
|
61 |
+
st.chat_message(msg["role"]).write(msg["content"])
|