tahirrashid commited on
Commit
72d0fa2
·
verified ·
1 Parent(s): 100dd46

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -0
app.py CHANGED
@@ -0,0 +1,48 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import requests
3
+
4
+ # App title
5
+ st.set_page_config(page_title="MSDS Emergency Chatbot")
6
+ st.title("🧯 MSDS Emergency Chatbot")
7
+ st.markdown("Get safety info and remedies for hazardous chemical incidents.")
8
+
9
+ # GROQ API setup
10
+ GROQ_API_KEY = st.secrets.get("GROQ_API_KEY") or "your-groq-api-key"
11
+ GROQ_MODEL = "llama3-70b-8192" # You can change the model if needed
12
+
13
+ # Function to query Groq API
14
+ def query_groq(prompt):
15
+ url = "https://api.groq.com/openai/v1/chat/completions"
16
+ headers = {
17
+ "Authorization": f"Bearer {GROQ_API_KEY}",
18
+ "Content-Type": "application/json"
19
+ }
20
+ payload = {
21
+ "model": GROQ_MODEL,
22
+ "messages": [
23
+ {"role": "system", "content": "You are an emergency response assistant. Use MSDS data to give concise and accurate safety information, remedies, and procedures in case of hazardous chemical spills, toxic gas releases, and fires."},
24
+ {"role": "user", "content": prompt}
25
+ ],
26
+ "temperature": 0.2
27
+ }
28
+ response = requests.post(url, headers=headers, json=payload)
29
+ response.raise_for_status()
30
+ return response.json()["choices"][0]["message"]["content"]
31
+
32
+ # Chat interface
33
+ if "chat_history" not in st.session_state:
34
+ st.session_state.chat_history = []
35
+
36
+ user_input = st.chat_input("Ask about a chemical emergency...")
37
+ if user_input:
38
+ st.session_state.chat_history.append(("user", user_input))
39
+ with st.spinner("Getting safety information..."):
40
+ reply = query_groq(user_input)
41
+ st.session_state.chat_history.append(("assistant", reply))
42
+
43
+ # Display conversation
44
+ for role, message in st.session_state.chat_history:
45
+ if role == "user":
46
+ st.chat_message("user").write(message)
47
+ else:
48
+ st.chat_message("assistant").write(message)