Richard Fan commited on
Commit
fcd099f
·
1 Parent(s): 293602f

update app

Browse files
Files changed (2) hide show
  1. src/app.py +41 -25
  2. src/utils.py +1 -0
src/app.py CHANGED
@@ -1,9 +1,11 @@
1
  import gradio as gr
2
  from download_new_papers import get_papers
 
3
  from relevancy import generate_relevance_score, process_subject_fields
4
  from sendgrid.helpers.mail import Mail, Email, To, Content
5
  import sendgrid
6
  import os
 
7
 
8
  topics = {
9
  "Physics": "",
@@ -72,6 +74,7 @@ def sample(email, topic, physics_topic, categories, interest):
72
  else:
73
  papers = get_papers(abbr, limit=4)
74
  if interest:
 
75
  relevancy, _ = generate_relevance_score(
76
  papers,
77
  query={"interest": interest},
@@ -86,7 +89,6 @@ def change_subsubject(subject, physics_subject):
86
  if subject != "Physics":
87
  return gr.Dropdown.update(choices=categories_map[subject], value=[], visible=True)
88
  else:
89
- print(physics_subject)
90
  if physics_subject and not isinstance(physics_subject, list):
91
  return gr.Dropdown.update(choices=categories_map[physics_subject], value=[], visible=True)
92
  else:
@@ -100,7 +102,9 @@ def change_physics(subject):
100
  return gr.Dropdown.update(physics_topics, visible=True)
101
 
102
 
103
- def test(email, topic, physics_topic, categories, interest):
 
 
104
  if topic == "Physics":
105
  if isinstance(physics_topic, list):
106
  raise gr.Error("You must choose a physics topic.")
@@ -116,19 +120,19 @@ def test(email, topic, physics_topic, categories, interest):
116
  else:
117
  papers = get_papers(abbr, limit=4)
118
  if interest:
 
119
  relevancy, hallucination = generate_relevance_score(
120
  papers,
121
  query={"interest": interest},
122
  threshold_score=7,
123
  num_paper_in_prompt=8)
124
- print(relevancy[0].keys())
125
  body = "<br><br>".join([f'Title: <a href="{paper["main_page"]}">{paper["title"]}</a><br>Authors: {paper["authors"]}<br>Score: {paper["Relevancy score"]}<br>Reason: {paper["Reasons for match"]}' for paper in relevancy])
126
  if hallucination:
127
  body = "Warning: the model hallucinated some papers. We have tried to remove them, but the scores may not be accurate.<br><br>" + body
128
  else:
129
  body = "<br><br>".join([f'Title: <a href="{paper["main_page"]}">{paper["title"]}</a><br>Authors: {paper["authors"]}' for paper in papers])
130
- sg = sendgrid.SendGridAPIClient(api_key=os.environ.get('SENDGRID_API_KEY'))
131
- from_email = Email("") # CHANGE TO YOUR VERIFIED SENDER
132
  to_email = To(email)
133
  subject = "arXiv digest"
134
  content = Content("text/html", body)
@@ -138,30 +142,42 @@ def test(email, topic, physics_topic, categories, interest):
138
  # Send an HTTP POST request to /mail/send
139
  response = sg.client.mail.send.post(request_body=mail_json)
140
  if response.status_code >= 200 and response.status_code <= 300:
141
- return "Send test email: Success!"
142
  else:
143
- return f"Send test email: Failure ({response.status_code})"
144
 
145
 
 
 
 
 
146
  with gr.Blocks() as demo:
147
- with gr.Column():
148
- email = gr.Textbox(label="Email address")
149
- subject = gr.Radio(
150
- list(topics.keys()), label="Topic to subscribe to"
151
- )
152
- physics_subject = gr.Dropdown(physics_topics, value=[], multiselect=False, label="Physics category", visible=False, info="")
153
- subsubject = gr.Dropdown(
154
- [], value=[], multiselect=True, label="Subtopic", info="", visible=False)
155
- subject.change(fn=change_physics, inputs=[subject], outputs=physics_subject)
156
- subject.change(fn=change_subsubject, inputs=[subject, physics_subject], outputs=subsubject)
157
- physics_subject.change(fn=change_subsubject, inputs=[subject, physics_subject], outputs=subsubject)
158
-
159
-
160
- interest = gr.Textbox(label="A natural language description of what you are interested in. Press enter to update.")
161
- sample_output = gr.Textbox(label="Examples")
162
- test_btn = gr.Button("Send email")
163
- output = gr.Textbox(label="Test email status")
164
- test_btn.click(fn=test, inputs=[email, subject, physics_subject, subsubject, interest], outputs=output)
 
 
 
 
 
 
 
 
165
  subject.change(fn=sample, inputs=[email, subject, physics_subject, subsubject, interest], outputs=sample_output)
166
  physics_subject.change(fn=sample, inputs=[email, subject, physics_subject, subsubject, interest], outputs=sample_output)
167
  subsubject.change(fn=sample, inputs=[email, subject, physics_subject, subsubject, interest], outputs=sample_output)
 
1
  import gradio as gr
2
  from download_new_papers import get_papers
3
+ import utils
4
  from relevancy import generate_relevance_score, process_subject_fields
5
  from sendgrid.helpers.mail import Mail, Email, To, Content
6
  import sendgrid
7
  import os
8
+ import openai
9
 
10
  topics = {
11
  "Physics": "",
 
74
  else:
75
  papers = get_papers(abbr, limit=4)
76
  if interest:
77
+ if not openai.api_key: raise gr.Error("Set your OpenAI api key on the left first")
78
  relevancy, _ = generate_relevance_score(
79
  papers,
80
  query={"interest": interest},
 
89
  if subject != "Physics":
90
  return gr.Dropdown.update(choices=categories_map[subject], value=[], visible=True)
91
  else:
 
92
  if physics_subject and not isinstance(physics_subject, list):
93
  return gr.Dropdown.update(choices=categories_map[physics_subject], value=[], visible=True)
94
  else:
 
102
  return gr.Dropdown.update(physics_topics, visible=True)
103
 
104
 
105
+ def test(email, topic, physics_topic, categories, interest, key):
106
+ if not email: raise gr.Error("Set your email")
107
+ if not key: raise gr.Error("Set your SendGrid key")
108
  if topic == "Physics":
109
  if isinstance(physics_topic, list):
110
  raise gr.Error("You must choose a physics topic.")
 
120
  else:
121
  papers = get_papers(abbr, limit=4)
122
  if interest:
123
+ if not openai.api_key: raise gr.Error("Set your OpenAI api key on the left first")
124
  relevancy, hallucination = generate_relevance_score(
125
  papers,
126
  query={"interest": interest},
127
  threshold_score=7,
128
  num_paper_in_prompt=8)
 
129
  body = "<br><br>".join([f'Title: <a href="{paper["main_page"]}">{paper["title"]}</a><br>Authors: {paper["authors"]}<br>Score: {paper["Relevancy score"]}<br>Reason: {paper["Reasons for match"]}' for paper in relevancy])
130
  if hallucination:
131
  body = "Warning: the model hallucinated some papers. We have tried to remove them, but the scores may not be accurate.<br><br>" + body
132
  else:
133
  body = "<br><br>".join([f'Title: <a href="{paper["main_page"]}">{paper["title"]}</a><br>Authors: {paper["authors"]}' for paper in papers])
134
+ sg = sendgrid.SendGridAPIClient(api_key=key)
135
+ from_email = Email(email)
136
  to_email = To(email)
137
  subject = "arXiv digest"
138
  content = Content("text/html", body)
 
142
  # Send an HTTP POST request to /mail/send
143
  response = sg.client.mail.send.post(request_body=mail_json)
144
  if response.status_code >= 200 and response.status_code <= 300:
145
+ return "Success!"
146
  else:
147
+ return "Failure: ({response.status_code})"
148
 
149
 
150
+ def register_openai_token(token):
151
+ print(f"registering new key: {token[:5]}")
152
+ openai.api_key = token
153
+
154
  with gr.Blocks() as demo:
155
+ with gr.Row():
156
+ with gr.Column(scale=0.40):
157
+ with gr.Box():
158
+ token = gr.Textbox(label="OpenAI API Key", type="password")
159
+ with gr.Box():
160
+ description = gr.HTML(value="Send an email to the below address using the configuration on the right. Requires a sendgrid token")
161
+ email = gr.Textbox(label="Email address", type="email", placeholder="")
162
+ sendgrid_token = gr.Textbox(label="SendGrid API Key", type="password")
163
+ with gr.Row():
164
+ test_btn = gr.Button("Send email")
165
+ output = gr.Textbox(show_label=False, placeholder="email status")
166
+ with gr.Column(scale=1):
167
+ subject = gr.Radio(
168
+ list(topics.keys()), label="Topic"
169
+ )
170
+ physics_subject = gr.Dropdown(physics_topics, value=[], multiselect=False, label="Physics category", visible=False, info="")
171
+ subsubject = gr.Dropdown(
172
+ [], value=[], multiselect=True, label="Subtopic", info="", visible=False)
173
+ subject.change(fn=change_physics, inputs=[subject], outputs=physics_subject)
174
+ subject.change(fn=change_subsubject, inputs=[subject, physics_subject], outputs=subsubject)
175
+ physics_subject.change(fn=change_subsubject, inputs=[subject, physics_subject], outputs=subsubject)
176
+
177
+ interest = gr.Textbox(label="A natural language description of what you are interested in. Press shift-enter to update.", lines=7)
178
+ sample_output = gr.Textbox(label="Examples")
179
+ test_btn.click(fn=test, inputs=[email, subject, physics_subject, subsubject, interest, sendgrid_token], outputs=output)
180
+ token.change(fn=register_openai_token, inputs=[token])
181
  subject.change(fn=sample, inputs=[email, subject, physics_subject, subsubject, interest], outputs=sample_output)
182
  physics_subject.change(fn=sample, inputs=[email, subject, physics_subject, subsubject, interest], outputs=sample_output)
183
  subsubject.change(fn=sample, inputs=[email, subject, physics_subject, subsubject, interest], outputs=sample_output)
src/utils.py CHANGED
@@ -15,6 +15,7 @@ import copy
15
 
16
  StrOrOpenAIObject = Union[str, openai_object.OpenAIObject]
17
 
 
18
  openai_org = os.getenv("OPENAI_ORG")
19
  if openai_org is not None:
20
  openai.organization = openai_org
 
15
 
16
  StrOrOpenAIObject = Union[str, openai_object.OpenAIObject]
17
 
18
+
19
  openai_org = os.getenv("OPENAI_ORG")
20
  if openai_org is not None:
21
  openai.organization = openai_org