Test_Elec_ChatBot / streamlit_app.py
adzee17's picture
Update streamlit_app.py
e4ec6de verified
import streamlit as st
import requests
import os
# Load your Groq API key from Hugging Face secret
GROQ_API_KEY = os.getenv("GROQ_API_KEY")
# System prompt for the chatbot
SYSTEM_PROMPT = """
You are an experienced automobile technician. Based on the user's question, provide:
1. Likely reasons for the vehicle issue
2. Diagnosis or explanation
3. Suggested fixes or preventive actions
Keep your answers simple, practical, and useful for everyday car owners.
"""
def query_groq(user_input):
url = "https://api.groq.com/openai/v1/chat/completions" # βœ… Correct endpoint
headers = {
"Authorization": f"Bearer {GROQ_API_KEY}",
"Content-Type": "application/json"
}
data = {
"model": "llama3-70b-8192", # βœ… Supported Groq model
"messages": [
{"role": "system", "content": SYSTEM_PROMPT},
{"role": "user", "content": user_input}
],
"temperature": 0.5,
"max_tokens": 500
}
try:
response = requests.post(url, headers=headers, json=data)
response.raise_for_status()
return response.json()["choices"][0]["message"]["content"]
except Exception as e:
return f"❌ Error: {str(e)}"
# Streamlit UI
st.set_page_config(page_title="Vehicle Diagnostic Chatbot", page_icon="πŸš—")
st.title("πŸš— Vehicle Issue Diagnostic Chatbot")
st.markdown("Describe your vehicle issue (e.g. *My car is overheating*) and get expert help.")
if "chat_history" not in st.session_state:
st.session_state.chat_history = []
with st.form("chat_form", clear_on_submit=True):
user_input = st.text_input("Describe your vehicle problem:")
submitted = st.form_submit_button("Get Diagnosis")
if submitted and user_input:
with st.spinner("Analyzing..."):
bot_reply = query_groq(user_input)
st.session_state.chat_history.append(("You", user_input))
st.session_state.chat_history.append(("Bot", bot_reply))
# Show chat history
for sender, message in st.session_state.chat_history:
st.markdown(f"**{sender}:** {message}")