Minerva666 commited on
Commit
7003d46
·
verified ·
1 Parent(s): 237f2c4

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +86 -0
app.py ADDED
@@ -0,0 +1,86 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import pandas as pd
3
+ from core import NHMQuery, run_structured_query
4
+ from agent import create_agent
5
+ from io import BytesIO
6
+
7
+ st.set_page_config("NHM GBIF Explorer", layout="wide")
8
+ st.title("NHM GBIF Explorer (LangChain)")
9
+
10
+ # ---------- API KEY ----------
11
+ st.sidebar.header("OpenAI")
12
+
13
+ openai_api_key = st.sidebar.text_input(
14
+ "OpenAI API Key",
15
+ type="password",
16
+ help="Used only for this session"
17
+ )
18
+
19
+ # ---------- STRUCTURED QUERY ----------
20
+ st.sidebar.header("Structured Query")
21
+
22
+ scientific_name = st.sidebar.text_input("Scientific name", "Quercus robur")
23
+ country = st.sidebar.text_input("Country (ISO)", "GB")
24
+ year = st.sidebar.text_input("Year range", "1800,1950")
25
+ limit = st.sidebar.number_input("Limit", 1, 500, 10)
26
+
27
+ if st.sidebar.button("Run Query"):
28
+
29
+ if not openai_api_key:
30
+ st.sidebar.error("OpenAI API key is required")
31
+ st.stop()
32
+
33
+ query = NHMQuery(
34
+ scientific_name=scientific_name,
35
+ country=country,
36
+ year=year,
37
+ limit=limit
38
+ )
39
+
40
+ result = run_structured_query(query)
41
+
42
+ st.session_state.df = result["example_records"]
43
+ st.session_state.summary = result["summary"]
44
+ st.session_state.filters = result["applied_filters"]
45
+ st.session_state.agent = create_agent(
46
+ st.session_state.df,
47
+ openai_api_key
48
+ )
49
+ st.session_state.chat = []
50
+
51
+ # ---------- DISPLAY ----------
52
+ if "df" in st.session_state:
53
+
54
+ st.subheader("Applied Filters")
55
+ st.json(st.session_state.filters)
56
+
57
+ st.subheader("Summary")
58
+ st.json(st.session_state.summary)
59
+
60
+ st.subheader("Records Table (Raw GBIF Data)")
61
+ st.dataframe(st.session_state.df)
62
+
63
+ st.subheader("Chat with the Data")
64
+
65
+ user_msg = st.chat_input("Ask about the table...")
66
+
67
+ if user_msg:
68
+ st.session_state.chat.append(("user", user_msg))
69
+ response = st.session_state.agent.run(user_msg)
70
+ st.session_state.chat.append(("assistant", response))
71
+
72
+ for role, msg in st.session_state.chat:
73
+ st.chat_message(role).write(msg)
74
+
75
+ st.subheader("Download Raw Data")
76
+
77
+ buffer = BytesIO()
78
+ st.session_state.df.to_excel(buffer, index=False, engine="openpyxl")
79
+ buffer.seek(0)
80
+
81
+ st.download_button(
82
+ label="Download raw GBIF data (Excel)",
83
+ data=buffer,
84
+ file_name="nhm_gbif_raw_data.xlsx",
85
+ mime="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
86
+ )