sunbal7 commited on
Commit
35fc402
Β·
verified Β·
1 Parent(s): 8ca2f9f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -15
app.py CHANGED
@@ -1,26 +1,30 @@
1
  import streamlit as st
2
  import requests
3
 
4
- # Open-source LLM endpoint (replace if needed)
5
- API_URL = "https://api-inference.huggingface.co/models/google/flan-t5-small"
 
6
 
7
- # If you have a Hugging Face token, include it here. If not, this model works without one.
8
- headers = {"Authorization": ""}
 
 
9
 
10
  def query_llm(prompt):
11
- payload = {"inputs": prompt}
12
- response = requests.post(API_URL, headers=headers, json=payload)
 
 
 
 
 
13
 
14
  try:
15
- output = response.json()
16
- if isinstance(output, list) and 'generated_text' in output[0]:
17
- return output[0]['generated_text']
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"❌ Failed to parse response: {str(e)}"
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 possible result."
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)