David Chu commited on
Commit
120e1e1
·
unverified ·
1 Parent(s): 8d10d04

fix: retry when semantic scholar req failed

Browse files
Files changed (1) hide show
  1. main.py +8 -5
main.py CHANGED
@@ -57,8 +57,10 @@ def generate_answer(
57
  return response.parsed
58
 
59
 
60
- def semantic_scholar(query: str, top_k: int = 10) -> list[Article]:
61
- resp = httpx.get(
 
 
62
  "https://api.semanticscholar.org/graph/v1/paper/search",
63
  params={
64
  "query": query,
@@ -106,7 +108,8 @@ def pubmed(query: str, top_k: int = 10, db: str = "pubmed"):
106
 
107
 
108
  def main():
109
- client = genai.Client(api_key=os.environ["GOOGLE_API_KEY"])
 
110
 
111
  st.title("Ask ~~Jeeves~~ Elna")
112
  with st.form("search", border=False):
@@ -116,14 +119,14 @@ def main():
116
 
117
  if submit:
118
  with st.spinner("Finding papers...", show_time=True):
119
- papers = semantic_scholar(query, top_k=30)
120
 
121
  if papers:
122
  with st.spinner("Thinking...", show_time=True):
123
  paper_map = {paper.id: paper for paper in papers}
124
  sentences = []
125
  citations = {}
126
- statements = generate_answer(client, query, papers)
127
  for statement in statements:
128
  if statement.citation:
129
  if not (
 
57
  return response.parsed
58
 
59
 
60
+ def semantic_scholar(
61
+ client: httpx.Client, query: str, top_k: int = 10
62
+ ) -> list[Article]:
63
+ resp = client.get(
64
  "https://api.semanticscholar.org/graph/v1/paper/search",
65
  params={
66
  "query": query,
 
108
 
109
 
110
  def main():
111
+ semantic_scholar_client = httpx.Client(transport=httpx.HTTPTransport(retries=1))
112
+ gemini_client = genai.Client(api_key=os.environ["GOOGLE_API_KEY"])
113
 
114
  st.title("Ask ~~Jeeves~~ Elna")
115
  with st.form("search", border=False):
 
119
 
120
  if submit:
121
  with st.spinner("Finding papers...", show_time=True):
122
+ papers = semantic_scholar(semantic_scholar_client, query, top_k=30)
123
 
124
  if papers:
125
  with st.spinner("Thinking...", show_time=True):
126
  paper_map = {paper.id: paper for paper in papers}
127
  sentences = []
128
  citations = {}
129
+ statements = generate_answer(gemini_client, query, papers)
130
  for statement in statements:
131
  if statement.citation:
132
  if not (