AI_Mechanic_Assistant / src /streamlit_app.py
krish-sai's picture
Update src/streamlit_app.py
f666bf8 verified
Raw History Blame Contribute Delete
2.47 kB
import streamlit as st
import requests
# --- Securely store your API keys here ---
OPENROUTER_API_KEY = "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" # Replace with your actual key
YOUTUBE_API_KEY = "AIzaSyCFE-fnv1hKm6VW7xN6RNGPSaSG4qKam94" # Replace with your actual YouTube Data API key
# --- Function to interact with OpenRouter API ---
def get_ai_solution(problem):
headers = {
'Authorization': f'Bearer {OPENROUTER_API_KEY}',
'Content-Type': 'application/json',
}
data = {
'model': 'mistralai/mistral-7b-instruct',
'messages': [
{
"role": "user",
"content": f"What are the common repair suggestions for this problem: '{problem}'"
}
]
}
response = requests.post("https://openrouter.ai/api/v1/chat/completions", headers=headers, json=data)
if response.status_code == 200:
result = response.json()
return result['choices'][0]['message']['content']
else:
return f"Error: {response.text}"
# --- Function to find YouTube video relevant to the problem ---
def find_youtube_video(problem):
search_url = "https://www.googleapis.com/youtube/v3/search"
params = {
"part": "snippet",
"q": problem,
"key": YOUTUBE_API_KEY,
"type": "video",
"maxResults": 1
}
response = requests.get(search_url, params=params)
if response.status_code == 200:
data = response.json()
if data["items"]:
video_id = data["items"][0]["id"]["videoId"]
return f"https://www.youtube.com/watch?v={video_id}"
return None
# --- Streamlit App UI ---
st.set_page_config(page_title="🔧 AI Mechanic Assistant", layout="centered")
st.title("🔧 AI Mechanic Assistant")
st.caption("Get AI-powered solutions and video tutorials for vehicle problems.")
problem = st.text_area("Describe your vehicle problem:")
if st.button("Get Solution"):
if problem.strip():
with st.spinner("Thinking..."):
solution = get_ai_solution(problem)
video_url = find_youtube_video(problem)
st.subheader("🧠 Suggested Fix:")
st.write(solution)
if video_url:
st.subheader("📺 Related Video:")
st.video(video_url)
else:
st.info("No related YouTube video found.")
else:
st.warning("Please describe the problem first.")