Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,26 +1,30 @@
|
|
1 |
import streamlit as st
|
2 |
import requests
|
3 |
|
4 |
-
#
|
5 |
-
|
|
|
6 |
|
7 |
-
|
8 |
-
|
|
|
|
|
9 |
|
10 |
def query_llm(prompt):
|
11 |
-
|
12 |
-
|
|
|
|
|
|
|
|
|
|
|
13 |
|
14 |
try:
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
elif isinstance(output, dict) and "error" in output:
|
19 |
-
return f"β API Error: {output['error']}"
|
20 |
-
else:
|
21 |
-
return "β Unexpected API response format."
|
22 |
except Exception as e:
|
23 |
-
return f"β
|
24 |
|
25 |
# Streamlit UI
|
26 |
st.set_page_config(page_title="Science Lab Assistant", page_icon="π§ͺ")
|
@@ -33,7 +37,7 @@ with st.form("lab_form"):
|
|
33 |
submitted = st.form_submit_button("Explain Experiment")
|
34 |
|
35 |
if submitted and experiment:
|
36 |
-
prompt = f"Explain the experiment '{experiment}' for a school science class. The student hypothesizes: '{hypothesis}'. Provide an explanation and
|
37 |
result = query_llm(prompt)
|
38 |
st.markdown("### π§ AI Explanation")
|
39 |
st.write(result)
|
|
|
1 |
import streamlit as st
|
2 |
import requests
|
3 |
|
4 |
+
# Set your OpenRouter API key here
|
5 |
+
API_KEY = "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" # π Replace with your key
|
6 |
+
API_URL = "https://openrouter.ai/api/v1/chat/completions"
|
7 |
|
8 |
+
HEADERS = {
|
9 |
+
"Authorization": f"Bearer {API_KEY}",
|
10 |
+
"Content-Type": "application/json"
|
11 |
+
}
|
12 |
|
13 |
def query_llm(prompt):
|
14 |
+
data = {
|
15 |
+
"model": "mistralai/mistral-7b-instruct",
|
16 |
+
"messages": [
|
17 |
+
{"role": "system", "content": "You are a helpful science teacher for school students."},
|
18 |
+
{"role": "user", "content": prompt}
|
19 |
+
]
|
20 |
+
}
|
21 |
|
22 |
try:
|
23 |
+
response = requests.post(API_URL, headers=HEADERS, json=data)
|
24 |
+
res = response.json()
|
25 |
+
return res["choices"][0]["message"]["content"]
|
|
|
|
|
|
|
|
|
26 |
except Exception as e:
|
27 |
+
return f"β Error: {str(e)}"
|
28 |
|
29 |
# Streamlit UI
|
30 |
st.set_page_config(page_title="Science Lab Assistant", page_icon="π§ͺ")
|
|
|
37 |
submitted = st.form_submit_button("Explain Experiment")
|
38 |
|
39 |
if submitted and experiment:
|
40 |
+
prompt = f"Explain the experiment '{experiment}' for a school science class. The student hypothesizes: '{hypothesis}'. Provide an explanation and expected results."
|
41 |
result = query_llm(prompt)
|
42 |
st.markdown("### π§ AI Explanation")
|
43 |
st.write(result)
|