Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import requests
|
| 3 |
+
import os
|
| 4 |
+
|
| 5 |
+
# Load API key from environment variable
|
| 6 |
+
GROQ_API_KEY = os.getenv("GROQ_API_KEY")
|
| 7 |
+
|
| 8 |
+
SYSTEM_PROMPT = """
|
| 9 |
+
You are an expert automobile technician. Based on the user's question, provide:
|
| 10 |
+
1. Likely reason(s) for the vehicle issue
|
| 11 |
+
2. A simple diagnosis
|
| 12 |
+
3. Suggested fixes or actions to prevent it
|
| 13 |
+
|
| 14 |
+
Keep your answer clear, short, and helpful for general car users.
|
| 15 |
+
"""
|
| 16 |
+
|
| 17 |
+
def query_groq(user_input):
|
| 18 |
+
url = "https://api.groq.com/openai/v1/chat/completions"
|
| 19 |
+
headers = {
|
| 20 |
+
"Authorization": f"Bearer {GROQ_API_KEY}",
|
| 21 |
+
"Content-Type": "application/json"
|
| 22 |
+
}
|
| 23 |
+
|
| 24 |
+
payload = {
|
| 25 |
+
"model": "llama2-70b-4096", # Or mixtral-8x7b-32768
|
| 26 |
+
"messages": [
|
| 27 |
+
{"role": "system", "content": SYSTEM_PROMPT},
|
| 28 |
+
{"role": "user", "content": user_input}
|
| 29 |
+
],
|
| 30 |
+
"temperature": 0.5,
|
| 31 |
+
"max_tokens": 500
|
| 32 |
+
}
|
| 33 |
+
|
| 34 |
+
try:
|
| 35 |
+
response = requests.post(url, headers=headers, json=payload)
|
| 36 |
+
response.raise_for_status()
|
| 37 |
+
return response.json()["choices"][0]["message"]["content"]
|
| 38 |
+
except Exception as e:
|
| 39 |
+
return f"β Error: {str(e)}"
|
| 40 |
+
|
| 41 |
+
# Streamlit UI
|
| 42 |
+
st.set_page_config(page_title="Vehicle Issue Chatbot", page_icon="π", layout="centered")
|
| 43 |
+
|
| 44 |
+
st.title("π Vehicle Fault Diagnostic Chatbot")
|
| 45 |
+
st.markdown("Describe your car issue (e.g. *my engine is overheating*) and get expert suggestions.")
|
| 46 |
+
|
| 47 |
+
if "chat_history" not in st.session_state:
|
| 48 |
+
st.session_state.chat_history = []
|
| 49 |
+
|
| 50 |
+
with st.form("chat_form", clear_on_submit=True):
|
| 51 |
+
user_input = st.text_input("Your vehicle issue:")
|
| 52 |
+
submitted = st.form_submit_button("Get Advice")
|
| 53 |
+
|
| 54 |
+
if submitted and user_input:
|
| 55 |
+
with st.spinner("Analyzing..."):
|
| 56 |
+
response = query_groq(user_input)
|
| 57 |
+
st.session_state.chat_history.append(("You", user_input))
|
| 58 |
+
st.session_state.chat_history.append(("Bot", response))
|
| 59 |
+
|
| 60 |
+
# Display chat history
|
| 61 |
+
for speaker, message in st.session_state.chat_history:
|
| 62 |
+
if speaker == "You":
|
| 63 |
+
st.markdown(f"**π§ You:** {message}")
|
| 64 |
+
else:
|
| 65 |
+
st.markdown(f"**π€ Bot:** {message}")
|