Spaces:
Sleeping
Sleeping
Update streamlit_app.py
Browse files- streamlit_app.py +17 -18
streamlit_app.py
CHANGED
|
@@ -2,12 +2,13 @@ import streamlit as st
|
|
| 2 |
import requests
|
| 3 |
import os
|
| 4 |
|
| 5 |
-
# Load Groq API key from Hugging Face secret
|
| 6 |
GROQ_API_KEY = os.getenv("GROQ_API_KEY")
|
| 7 |
|
|
|
|
| 8 |
SYSTEM_PROMPT = """
|
| 9 |
You are an experienced automobile technician. Based on the user's question, provide:
|
| 10 |
-
1. Likely
|
| 11 |
2. Diagnosis or explanation
|
| 12 |
3. Suggested fixes or preventive actions
|
| 13 |
|
|
@@ -15,14 +16,15 @@ Keep your answers simple, practical, and useful for everyday car owners.
|
|
| 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 |
data = {
|
| 25 |
-
"model": "
|
| 26 |
"messages": [
|
| 27 |
{"role": "system", "content": SYSTEM_PROMPT},
|
| 28 |
{"role": "user", "content": user_input}
|
|
@@ -38,27 +40,24 @@ def query_groq(user_input):
|
|
| 38 |
except Exception as e:
|
| 39 |
return f"β Error: {str(e)}"
|
| 40 |
|
| 41 |
-
# Streamlit
|
| 42 |
-
st.set_page_config(page_title="Vehicle Diagnostic Chatbot", page_icon="
|
| 43 |
-
st.title("
|
| 44 |
-
st.markdown("Describe your
|
| 45 |
|
| 46 |
if "chat_history" not in st.session_state:
|
| 47 |
st.session_state.chat_history = []
|
| 48 |
|
| 49 |
with st.form("chat_form", clear_on_submit=True):
|
| 50 |
-
user_input = st.text_input("
|
| 51 |
-
submitted = st.form_submit_button("
|
| 52 |
|
| 53 |
if submitted and user_input:
|
| 54 |
-
with st.spinner("Analyzing
|
| 55 |
-
|
| 56 |
st.session_state.chat_history.append(("You", user_input))
|
| 57 |
-
st.session_state.chat_history.append(("Bot",
|
| 58 |
|
| 59 |
-
#
|
| 60 |
for sender, message in st.session_state.chat_history:
|
| 61 |
-
|
| 62 |
-
st.markdown(f"**π§ You:** {message}")
|
| 63 |
-
else:
|
| 64 |
-
st.markdown(f"**π€ Bot:** {message}")
|
|
|
|
| 2 |
import requests
|
| 3 |
import os
|
| 4 |
|
| 5 |
+
# Load your Groq API key from Hugging Face secret
|
| 6 |
GROQ_API_KEY = os.getenv("GROQ_API_KEY")
|
| 7 |
|
| 8 |
+
# System prompt for the chatbot
|
| 9 |
SYSTEM_PROMPT = """
|
| 10 |
You are an experienced automobile technician. Based on the user's question, provide:
|
| 11 |
+
1. Likely reasons for the vehicle issue
|
| 12 |
2. Diagnosis or explanation
|
| 13 |
3. Suggested fixes or preventive actions
|
| 14 |
|
|
|
|
| 16 |
"""
|
| 17 |
|
| 18 |
def query_groq(user_input):
|
| 19 |
+
url = "https://api.groq.com/openai/v1/chat/completions" # β
Correct endpoint
|
| 20 |
+
|
| 21 |
headers = {
|
| 22 |
"Authorization": f"Bearer {GROQ_API_KEY}",
|
| 23 |
"Content-Type": "application/json"
|
| 24 |
}
|
| 25 |
|
| 26 |
data = {
|
| 27 |
+
"model": "llama3-70b-8192", # β
Supported Groq model
|
| 28 |
"messages": [
|
| 29 |
{"role": "system", "content": SYSTEM_PROMPT},
|
| 30 |
{"role": "user", "content": user_input}
|
|
|
|
| 40 |
except Exception as e:
|
| 41 |
return f"β Error: {str(e)}"
|
| 42 |
|
| 43 |
+
# Streamlit UI
|
| 44 |
+
st.set_page_config(page_title="Vehicle Diagnostic Chatbot", page_icon="π")
|
| 45 |
+
st.title("π Vehicle Issue Diagnostic Chatbot")
|
| 46 |
+
st.markdown("Describe your vehicle issue (e.g. *My car is overheating*) and get expert help.")
|
| 47 |
|
| 48 |
if "chat_history" not in st.session_state:
|
| 49 |
st.session_state.chat_history = []
|
| 50 |
|
| 51 |
with st.form("chat_form", clear_on_submit=True):
|
| 52 |
+
user_input = st.text_input("Describe your vehicle problem:")
|
| 53 |
+
submitted = st.form_submit_button("Get Diagnosis")
|
| 54 |
|
| 55 |
if submitted and user_input:
|
| 56 |
+
with st.spinner("Analyzing..."):
|
| 57 |
+
bot_reply = query_groq(user_input)
|
| 58 |
st.session_state.chat_history.append(("You", user_input))
|
| 59 |
+
st.session_state.chat_history.append(("Bot", bot_reply))
|
| 60 |
|
| 61 |
+
# Show chat history
|
| 62 |
for sender, message in st.session_state.chat_history:
|
| 63 |
+
st.markdown(f"**{sender}:** {message}")
|
|
|
|
|
|
|
|
|