David Chu
commited on
refactor: improve process readibility
Browse files
main.py
CHANGED
@@ -112,17 +112,17 @@ def main():
|
|
112 |
with st.form("search", border=False):
|
113 |
query = st.text_input("Your medical question")
|
114 |
submit = st.form_submit_button("Ask")
|
|
|
115 |
|
116 |
if submit:
|
117 |
-
answer = []
|
118 |
-
citations = {}
|
119 |
-
footnotes = ""
|
120 |
with st.spinner("Finding papers...", show_time=True):
|
121 |
papers = semantic_scholar(query, top_k=30)
|
122 |
|
123 |
if papers:
|
124 |
with st.spinner("Thinking...", show_time=True):
|
125 |
paper_map = {paper.id: paper for paper in papers}
|
|
|
|
|
126 |
statements = generate_answer(client, query, papers)
|
127 |
for statement in statements:
|
128 |
if statement.citation:
|
@@ -133,20 +133,22 @@ def main():
|
|
133 |
):
|
134 |
citation_id = len(citations) + 1
|
135 |
citations[statement.citation.source_id] = citation_id
|
136 |
-
|
137 |
else:
|
138 |
-
|
139 |
-
answer = " ".join(
|
140 |
-
|
141 |
if citations:
|
142 |
footnotes = "\n".join(
|
143 |
f"[^{v}]: [{paper_map[k].title}](https://doi.org/{paper_map[k].id})"
|
144 |
for k, v in citations.items()
|
145 |
)
|
146 |
|
147 |
-
|
148 |
-
|
149 |
-
|
|
|
|
|
150 |
|
151 |
|
152 |
if __name__ == "__main__":
|
|
|
112 |
with st.form("search", border=False):
|
113 |
query = st.text_input("Your medical question")
|
114 |
submit = st.form_submit_button("Ask")
|
115 |
+
response = st.empty()
|
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:
|
|
|
133 |
):
|
134 |
citation_id = len(citations) + 1
|
135 |
citations[statement.citation.source_id] = citation_id
|
136 |
+
sentences.append(f"{statement.text}[^{citation_id}]")
|
137 |
else:
|
138 |
+
sentences.append(statement.text)
|
139 |
+
answer = " ".join(sentences)
|
140 |
+
footnotes = ""
|
141 |
if citations:
|
142 |
footnotes = "\n".join(
|
143 |
f"[^{v}]: [{paper_map[k].title}](https://doi.org/{paper_map[k].id})"
|
144 |
for k, v in citations.items()
|
145 |
)
|
146 |
|
147 |
+
result = f"{answer}\n\n{footnotes}"
|
148 |
+
else:
|
149 |
+
result = "No relevant papers found."
|
150 |
+
|
151 |
+
response.markdown(result)
|
152 |
|
153 |
|
154 |
if __name__ == "__main__":
|