Spaces:
Runtime error
Runtime error
Commit
·
c881d34
1
Parent(s):
d410e1c
Update app.py
Browse files
app.py
CHANGED
@@ -6,6 +6,10 @@ import requests
|
|
6 |
import os
|
7 |
|
8 |
HF_API = os.getenv('HF_API')
|
|
|
|
|
|
|
|
|
9 |
|
10 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
11 |
import torch
|
@@ -63,33 +67,81 @@ def search_pubmed(query, retmax):
|
|
63 |
article_list.append(article_dict)
|
64 |
return pd.DataFrame(article_list)
|
65 |
|
66 |
-
# Function to
|
67 |
-
def
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
print(
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
payload = {
|
81 |
-
"inputs": text_to_summarize,
|
82 |
-
"parameters": {"max_length": 300} # Adjust as needed
|
83 |
}
|
84 |
|
85 |
-
|
86 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
87 |
else:
|
88 |
-
#
|
89 |
-
response
|
90 |
-
response.
|
91 |
-
|
92 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
93 |
|
94 |
|
95 |
|
@@ -119,7 +171,6 @@ def summarize_articles(indices, articles_for_display):
|
|
119 |
summary = summarize_with_huggingface(selected_articles)
|
120 |
return summary
|
121 |
|
122 |
-
PASSWORD = "pass"
|
123 |
|
124 |
def check_password(username, password):
|
125 |
if username == USERNAME and password == PASSWORD:
|
@@ -133,7 +184,9 @@ with gr.Blocks() as demo:
|
|
133 |
gr.Markdown("### PubMed Article Summarizer")
|
134 |
|
135 |
|
136 |
-
|
|
|
|
|
137 |
query_input = gr.Textbox(label="Query Keywords")
|
138 |
retmax_input = gr.Slider(minimum=1, maximum=20, value=5, step=1, label="Number of articles")
|
139 |
search_button = gr.Button("Search")
|
@@ -147,7 +200,7 @@ with gr.Blocks() as demo:
|
|
147 |
# output_table.update(value=df)
|
148 |
return df
|
149 |
search_button.click(update_output_table, inputs=[query_input, retmax_input], outputs=output_table)
|
150 |
-
summarize_button.click(fn=summarize_with_huggingface, inputs=[model_input, output_table], outputs=summary_output)
|
151 |
|
152 |
demo.launch(debug=True)
|
153 |
|
|
|
6 |
import os
|
7 |
|
8 |
HF_API = os.getenv('HF_API')
|
9 |
+
openai_api_key = os.getenv('OPENAI_API')
|
10 |
+
|
11 |
+
PASSWORD = "pass"
|
12 |
+
|
13 |
|
14 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
15 |
import torch
|
|
|
67 |
article_list.append(article_dict)
|
68 |
return pd.DataFrame(article_list)
|
69 |
|
70 |
+
# Function to format search results for OpenAI summarization
|
71 |
+
def format_results_for_openai(table_data):
|
72 |
+
# Combine title and abstract for each record into one string for summarization
|
73 |
+
summaries = []
|
74 |
+
for _, row in table_data.iterrows():
|
75 |
+
summary = f"Title: {row['Title']}\nAuthors:{row['Authors']}\nAbstract: {row['Abstract']}\n"
|
76 |
+
summaries.append(summary)
|
77 |
+
print(summaries)
|
78 |
+
return "\n".join(summaries)
|
79 |
+
|
80 |
+
def get_summary_from_openai(text_to_summarize, openai_api_key):
|
81 |
+
headers = {
|
82 |
+
'Authorization': f'Bearer {openai_api_key}',
|
83 |
+
'Content-Type': 'application/json'
|
|
|
|
|
|
|
84 |
}
|
85 |
|
86 |
+
data = {
|
87 |
+
"model": "gpt-3.5-turbo", # Specify the GPT-3.5-turbo model
|
88 |
+
"messages": [{"role": "system", "content": '''Please summarize the following PubMed search results,
|
89 |
+
including the authors who conducted the research, the main research subject, and the major findings.
|
90 |
+
Please compare the difference among these articles.
|
91 |
+
Please return your results in a single paragraph in the regular scientific paper fashion for each article:'''},
|
92 |
+
{"role": "user", "content": text_to_summarize}],
|
93 |
+
}
|
94 |
+
|
95 |
+
response = requests.post('https://api.openai.com/v1/chat/completions', headers=headers, json=data)
|
96 |
+
|
97 |
+
if response.status_code == 200:
|
98 |
+
summary = response.json().get('choices', [{}])[0].get('message', {'content':''}).get('content', '').strip()
|
99 |
+
return summary
|
100 |
else:
|
101 |
+
# Print the error message if the API call was unsuccessful
|
102 |
+
print(f"Error: {response.status_code}")
|
103 |
+
print(response.text)
|
104 |
+
return None
|
105 |
+
|
106 |
+
# Function that combines PubMed search with OpenAI summarization
|
107 |
+
def summarize_pubmed_search(search_results):
|
108 |
+
formatted_text = format_results_for_openai(search_results)
|
109 |
+
summary = get_summary_from_openai(formatted_text, openai_api_key) # Replace with your actual OpenAI API key
|
110 |
+
return summary
|
111 |
+
|
112 |
+
# Function to summarize articles using Hugging Face's API
|
113 |
+
def summarize_with_huggingface(model, selected_articles, password):
|
114 |
+
|
115 |
+
if password == PASSWORD:
|
116 |
+
summary = summarize_pubmed_search(format_results_for_openai(selected_articles),openai_api_key)
|
117 |
+
return summary
|
118 |
+
else:
|
119 |
+
API_URL = f"https://api-inference.huggingface.co/models/{model}"
|
120 |
+
# Your Hugging Face API key
|
121 |
+
API_KEY = HF_API
|
122 |
+
headers = {"Authorization": f"Bearer {API_KEY}"}
|
123 |
+
# Prepare the text to summarize: concatenate all abstracts
|
124 |
+
print(type(selected_articles))
|
125 |
+
print(selected_articles.to_dict(orient='records'))
|
126 |
+
text_to_summarize = " ".join(
|
127 |
+
[f"PMID: {article['PMID']}. Authors: {article['Authors']}. Title: {article['Title']}. Abstract: {article['Abstract']}."
|
128 |
+
for article in selected_articles.to_dict(orient='records')]
|
129 |
+
)
|
130 |
+
# Define the payload
|
131 |
+
payload = {
|
132 |
+
"inputs": text_to_summarize,
|
133 |
+
"parameters": {"max_length": 300} # Adjust as needed
|
134 |
+
}
|
135 |
+
|
136 |
+
USE_LOCAL=False
|
137 |
+
if USE_LOCAL:
|
138 |
+
response = generate_response(text_to_summarize)
|
139 |
+
else:
|
140 |
+
# Make the POST request to the Hugging Face API
|
141 |
+
response = requests.post(API_URL, headers=headers, json=payload)
|
142 |
+
response.raise_for_status() # Raise an HTTPError if the HTTP request returned an unsuccessful status code
|
143 |
+
# The API returns a list of dictionaries. We extract the summary from the first one.
|
144 |
+
return response.json()[0]['generated_text']
|
145 |
|
146 |
|
147 |
|
|
|
171 |
summary = summarize_with_huggingface(selected_articles)
|
172 |
return summary
|
173 |
|
|
|
174 |
|
175 |
def check_password(username, password):
|
176 |
if username == USERNAME and password == PASSWORD:
|
|
|
184 |
gr.Markdown("### PubMed Article Summarizer")
|
185 |
|
186 |
|
187 |
+
with gr.Row():
|
188 |
+
password_input = gr.Textbox(label="Enter the password")
|
189 |
+
model_input = gr.Textbox(label="Enter the model to use", value="h2oai/h2ogpt-4096-llama2-7b-chat")
|
190 |
query_input = gr.Textbox(label="Query Keywords")
|
191 |
retmax_input = gr.Slider(minimum=1, maximum=20, value=5, step=1, label="Number of articles")
|
192 |
search_button = gr.Button("Search")
|
|
|
200 |
# output_table.update(value=df)
|
201 |
return df
|
202 |
search_button.click(update_output_table, inputs=[query_input, retmax_input], outputs=output_table)
|
203 |
+
summarize_button.click(fn=summarize_with_huggingface, inputs=[model_input, output_table, password_input], outputs=summary_output)
|
204 |
|
205 |
demo.launch(debug=True)
|
206 |
|