shaistaDev7 commited on
Commit
a748c4c
·
verified ·
1 Parent(s): daf335d

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +240 -0
app.py ADDED
@@ -0,0 +1,240 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import json
3
+ import logging
4
+ import requests
5
+ import xmltodict
6
+ import time
7
+ import streamlit as st
8
+ from openai import OpenAI
9
+ from typing import List, Dict
10
+ from io import StringIO
11
+
12
+ # Configure logging for progress tracking and debugging
13
+ logging.basicConfig(level=logging.INFO)
14
+ logger = logging.getLogger(__name__)
15
+
16
+ # Initialize OpenAI client with the DeepSeek model
17
+ client = OpenAI(
18
+ base_url="https://api.aimlapi.com/v1",
19
+ api_key="api-key", # Replace with your AIML API key
20
+ )
21
+
22
+ # Define constants for PubMed API
23
+ BASE_URL = "https://eutils.ncbi.nlm.nih.gov/entrez/eutils/"
24
+ SEARCH_URL = f"{BASE_URL}esearch.fcgi"
25
+ FETCH_URL = f"{BASE_URL}efetch.fcgi"
26
+
27
+ class KnowledgeBaseLoader:
28
+ """
29
+ Loads schizophrenia research documents from a JSON file.
30
+ """
31
+ def __init__(self, filepath: str):
32
+ self.filepath = filepath
33
+
34
+ def load_data(self) -> List[Dict]:
35
+ """Loads and returns data from the JSON file."""
36
+ try:
37
+ with open(self.filepath, "r", encoding="utf-8") as f:
38
+ data = json.load(f)
39
+ logger.info(f"Successfully loaded {len(data)} records from '{self.filepath}'.")
40
+ return data
41
+ except Exception as e:
42
+ logger.error(f"Error loading knowledge base: {e}")
43
+ return []
44
+
45
+ class SchizophreniaAgent:
46
+ """
47
+ An agent to answer questions related to schizophrenia using a domain-specific knowledge base.
48
+ """
49
+ def __init__(self, knowledge_base: List[Dict]):
50
+ self.knowledge_base = knowledge_base
51
+
52
+ def process_query(self, query: str) -> str:
53
+ """
54
+ Process the incoming query by searching for matching documents in the knowledge base.
55
+
56
+ Args:
57
+ query: A string containing the user's query.
58
+
59
+ Returns:
60
+ A response string summarizing how many documents matched and some sample content.
61
+ """
62
+ if not self.knowledge_base:
63
+ logger.warning("Knowledge base is empty. Cannot process query.")
64
+ return "No knowledge base available."
65
+
66
+ # Simple matching: count documents where query text is found in abstract
67
+ matching_docs = []
68
+
69
+ for doc in self.knowledge_base:
70
+ # Ensure abstract is a string (if it's a list, join it into a single string)
71
+ abstract = doc.get("abstract", [])
72
+
73
+ # Check if abstract is a list and join items that are strings
74
+ if isinstance(abstract, list):
75
+ abstract = " ".join([str(item) for item in abstract if isinstance(item, str)]).strip()
76
+
77
+ if query.lower() in abstract.lower():
78
+ matching_docs.append(doc)
79
+
80
+ logger.info(f"Query '{query}' matched {len(matching_docs)} documents.")
81
+
82
+ # For a more robust agent, integrate with an LLM or retrieval system here.
83
+ if len(matching_docs) > 0:
84
+ response = (
85
+ f"Found {len(matching_docs)} documents matching your query. "
86
+ f"Examples: " +
87
+ ", ".join(f"'{doc.get('title', 'No Title')}'" for doc in matching_docs[:3]) +
88
+ "."
89
+ )
90
+ else:
91
+ response = "No relevant documents found for your query."
92
+
93
+ # Now ask the AIML model (DeepSeek) to generate more user-friendly information
94
+ aiml_response = self.query_deepseek(query)
95
+ return response + "\n\nAI-Suggested Guidance:\n" + aiml_response
96
+
97
+ def query_deepseek(self, query: str) -> str:
98
+ """Query DeepSeek for additional AI-driven responses."""
99
+ response = client.chat.completions.create(
100
+ model="deepseek/deepseek-r1",
101
+ messages=[
102
+ {"role": "system", "content": "You are an AI assistant who knows everything about schizophrenia."},
103
+ {"role": "user", "content": query}
104
+ ],
105
+ )
106
+ return response.choices[0].message.content
107
+
108
+ def fetch_pubmed_papers(query: str, max_results: int = 10):
109
+ """
110
+ Fetch PubMed papers related to the query (e.g., "schizophrenia").
111
+
112
+ Args:
113
+ query (str): The search term to look for in PubMed.
114
+ max_results (int): The maximum number of results to fetch (default is 10).
115
+
116
+ Returns:
117
+ List of dictionaries containing paper details like title, abstract, etc.
118
+ """
119
+ # Step 1: Search PubMed for articles related to the query
120
+ search_params = {
121
+ 'db': 'pubmed',
122
+ 'term': query,
123
+ 'retmax': max_results,
124
+ 'retmode': 'xml'
125
+ }
126
+ search_response = requests.get(SEARCH_URL, params=search_params)
127
+
128
+ if search_response.status_code != 200:
129
+ print("Error: Unable to fetch search results from PubMed.")
130
+ return []
131
+
132
+ search_data = xmltodict.parse(search_response.text)
133
+
134
+ # Step 2: Extract PubMed IDs (PMIDs) from the search results
135
+ try:
136
+ pmids = search_data['eSearchResult']['IdList']['Id']
137
+ except KeyError:
138
+ print("Error: No PubMed IDs found in search results.")
139
+ return []
140
+
141
+ # Step 3: Fetch the details of the papers using the PMIDs
142
+ papers = []
143
+ for pmid in pmids:
144
+ fetch_params = {
145
+ 'db': 'pubmed',
146
+ 'id': pmid,
147
+ 'retmode': 'xml',
148
+ 'rettype': 'abstract'
149
+ }
150
+ fetch_response = requests.get(FETCH_URL, params=fetch_params)
151
+
152
+ if fetch_response.status_code != 200:
153
+ print(f"Error: Unable to fetch details for PMID {pmid}")
154
+ continue
155
+
156
+ fetch_data = xmltodict.parse(fetch_response.text)
157
+
158
+ # Extract relevant details for each paper
159
+ try:
160
+ paper = fetch_data['PubmedArticleSet']['PubmedArticle']
161
+ title = paper['MedlineCitation']['Article']['ArticleTitle']
162
+ abstract = paper['MedlineCitation']['Article'].get('Abstract', {}).get('AbstractText', 'No abstract available.')
163
+ journal = paper['MedlineCitation']['Article']['Journal']['Title']
164
+ year = paper['MedlineCitation']['Article']['Journal']['JournalIssue']['PubDate']['Year']
165
+
166
+ # Store paper details in a dictionary
167
+ papers.append({
168
+ 'pmid': pmid,
169
+ 'title': title,
170
+ 'abstract': abstract,
171
+ 'journal': journal,
172
+ 'year': year
173
+ })
174
+ except KeyError:
175
+ print(f"Error parsing paper details for PMID {pmid}")
176
+ continue
177
+
178
+ # Add a delay between requests to avoid hitting rate limits
179
+ time.sleep(1)
180
+
181
+ return papers
182
+
183
+ # Streamlit User Interface
184
+ def main():
185
+ # Set configuration: path to the parsed knowledge base file
186
+ data_file = os.getenv("SCHIZ_DATA_FILE", "parsed_data.json")
187
+
188
+ # Initialize and load the knowledge base
189
+ loader = KnowledgeBaseLoader(data_file)
190
+ kb_data = loader.load_data()
191
+
192
+ # Initialize the schizophrenia agent with the loaded data
193
+ agent = SchizophreniaAgent(knowledge_base=kb_data)
194
+
195
+ # Streamlit UI setup
196
+ st.set_page_config(page_title="Schizophrenia Assistant", page_icon="🧠", layout="wide")
197
+
198
+ st.title("Schizophrenia Episode Management Assistant")
199
+ st.markdown(
200
+ """
201
+ This tool helps you manage schizophrenia episodes. You can search PubMed for research papers or provide details about a patient's episode, and the assistant will provide recommendations and guidance.
202
+ """
203
+ )
204
+
205
+ # **Part 1: Fetch and Download PubMed Papers**
206
+ st.header("Fetch and Download PubMed Papers")
207
+ query = st.text_input("Enter search query (e.g., schizophrenia):", value="schizophrenia")
208
+ if st.button("Fetch PubMed Papers"):
209
+ with st.spinner("Fetching papers..."):
210
+ papers = fetch_pubmed_papers(query, max_results=10)
211
+ if papers:
212
+ # Save papers to JSON and provide download link
213
+ json_data = json.dumps(papers, ensure_ascii=False, indent=4)
214
+ st.download_button("Download JSON", data=json_data, file_name="pubmed_papers.json", mime="application/json")
215
+ st.success(f"Successfully fetched {len(papers)} papers related to '{query}'")
216
+ else:
217
+ st.error("No papers found. Please try another query.")
218
+
219
+ # **Part 2: Upload and Use JSON File**
220
+ st.header("Upload and Use JSON File for Schizophrenia Assistant")
221
+ uploaded_file = st.file_uploader("Upload PubMed JSON file", type=["json"])
222
+ if uploaded_file is not None:
223
+ file_data = json.load(uploaded_file)
224
+ st.write("File uploaded successfully. You can now query the assistant.")
225
+ agent = SchizophreniaAgent(knowledge_base=file_data)
226
+
227
+ # User Input for Query
228
+ user_input = st.text_area("Enter the patient's condition or episode details:", height=200)
229
+ if st.button("Get Response"):
230
+ if user_input.strip():
231
+ with st.spinner("Processing your request..."):
232
+ answer = agent.process_query(user_input.strip())
233
+ st.subheader("Response")
234
+ st.write(answer)
235
+ else:
236
+ st.error("Please enter a valid query to get a response.")
237
+
238
+ # Run the Streamlit app
239
+ if __name__ == "__main__":
240
+ main()