Spaces:
Running
Running
Commit
·
9e062cd
1
Parent(s):
3ec5aa6
Implement email sending functionality and add Gradio interface for ReXplore backend
Browse files
app.py
ADDED
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from main import post_blogpost
|
3 |
+
|
4 |
+
def clear_everything(title, category, summary, mindmap, citation, status, access_key):
|
5 |
+
return None, None, None, None, None, None, None
|
6 |
+
|
7 |
+
theme = gr.themes.Soft(
|
8 |
+
primary_hue="purple",
|
9 |
+
secondary_hue="cyan",
|
10 |
+
neutral_hue="slate",
|
11 |
+
font=[
|
12 |
+
gr.themes.GoogleFont("Syne"),
|
13 |
+
gr.themes.GoogleFont("Poppins"),
|
14 |
+
gr.themes.GoogleFont("Poppins"),
|
15 |
+
gr.themes.GoogleFont("Poppins")
|
16 |
+
],
|
17 |
+
)
|
18 |
+
|
19 |
+
with gr.Blocks(theme=theme, title="ReXplore Backend", fill_height=True) as app:
|
20 |
+
gr.HTML(
|
21 |
+
value ="""
|
22 |
+
<h1 style="text-align: center;">ReXplore Backend<p style="text-align: center;">Designed and Developed by <a href="https://raannakasturi.eu.org" target="_blank" rel="nofollow noreferrer external">Nayan Kasturi</a></p> </h1>
|
23 |
+
<p style="text-align: center;">Backend for ReXplore</p>
|
24 |
+
""")
|
25 |
+
with gr.Row():
|
26 |
+
with gr.Column():
|
27 |
+
access_key = gr.Textbox(label="Access Key", placeholder="Enter the Access Key", type="password")
|
28 |
+
start = gr.Button(value="Start", variant="primary")
|
29 |
+
status = gr.Textbox(label="Data", placeholder="Enter the data", lines=7)
|
30 |
+
start.click(
|
31 |
+
post_blogpost,
|
32 |
+
inputs=[access_key],
|
33 |
+
outputs=[status],
|
34 |
+
concurrency_limit=25,
|
35 |
+
scroll_to_output=True,
|
36 |
+
show_api=True,
|
37 |
+
api_name="rexplore_backend",
|
38 |
+
show_progress="full",
|
39 |
+
)
|
40 |
+
|
41 |
+
app.queue(default_concurrency_limit=25).launch(show_api=True)
|
main.py
CHANGED
@@ -1,6 +1,13 @@
|
|
1 |
import json
|
2 |
import os
|
3 |
import dotenv
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
from summarize_paper import summarize_paper
|
5 |
from fetch_data import fetch_paper_data_with_category
|
6 |
from post_blog import post_blog
|
@@ -9,6 +16,7 @@ from post_blog import post_blog
|
|
9 |
dotenv.load_dotenv()
|
10 |
cat_ids_api_key = os.getenv("DATA_API_ACCESS_KEY")
|
11 |
summarizer_api_key = os.getenv("SUMMARIZER_API_KEY")
|
|
|
12 |
|
13 |
def paper_data(paper_data):
|
14 |
data = {"status": "success"}
|
@@ -43,9 +51,44 @@ def paper_data(paper_data):
|
|
43 |
print(f"Processed data saved to {output_file}")
|
44 |
return data
|
45 |
|
46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
47 |
data = fetch_paper_data_with_category(cat_ids_api_key)
|
48 |
-
with open("paper_data.json", "w", encoding="utf-8") as file:
|
49 |
-
json.dump(data, file, indent=4, ensure_ascii=False)
|
50 |
pdata = paper_data(data)
|
51 |
-
|
|
|
|
1 |
import json
|
2 |
import os
|
3 |
import dotenv
|
4 |
+
from email.mime.text import MIMEText
|
5 |
+
from email.mime.multipart import MIMEMultipart
|
6 |
+
from email.mime.base import MIMEBase
|
7 |
+
from email import encoders
|
8 |
+
import sib_api_v3_sdk
|
9 |
+
from sib_api_v3_sdk.rest import ApiException
|
10 |
+
|
11 |
from summarize_paper import summarize_paper
|
12 |
from fetch_data import fetch_paper_data_with_category
|
13 |
from post_blog import post_blog
|
|
|
16 |
dotenv.load_dotenv()
|
17 |
cat_ids_api_key = os.getenv("DATA_API_ACCESS_KEY")
|
18 |
summarizer_api_key = os.getenv("SUMMARIZER_API_KEY")
|
19 |
+
mail_api = os.getenv("MAIL_API")
|
20 |
|
21 |
def paper_data(paper_data):
|
22 |
data = {"status": "success"}
|
|
|
51 |
print(f"Processed data saved to {output_file}")
|
52 |
return data
|
53 |
|
54 |
+
def create_attachment(content, filename):
|
55 |
+
attachment = MIMEBase('application', 'octet-stream')
|
56 |
+
attachment.set_payload(content)
|
57 |
+
encoders.encode_base64(attachment)
|
58 |
+
attachment.add_header('Content-Disposition', f'attachment; filename="{filename}"')
|
59 |
+
return attachment
|
60 |
+
|
61 |
+
def send_email(data):
|
62 |
+
configuration = sib_api_v3_sdk.Configuration()
|
63 |
+
configuration.api_key['api-key'] = mail_api
|
64 |
+
api_instance = sib_api_v3_sdk.TransactionalEmailsApi(sib_api_v3_sdk.ApiClient(configuration))
|
65 |
+
|
66 |
+
data = data
|
67 |
+
data_attachment = create_attachment(data.encode('utf-8'), "data.json")
|
68 |
+
|
69 |
+
subject = "ReXplore - Blogs Generated. Here are the details"
|
70 |
+
sender = {"name": "Project Gatekeeper", "email": "[email protected]"}
|
71 |
+
reply_to = {"name": "Project Gatekeeper", "email": "[email protected]"}
|
72 |
+
text_content = data
|
73 |
+
attachments = [
|
74 |
+
{"content": data_attachment.get_payload(), "name": data_attachment.get_filename()},
|
75 |
+
]
|
76 |
+
to = [{"email": "[email protected]"}]
|
77 |
+
send_smtp_email = sib_api_v3_sdk.SendSmtpEmail(to=to, reply_to=reply_to, attachment=attachments, text_content=text_content, sender=sender, subject=subject)
|
78 |
+
try:
|
79 |
+
api_response = api_instance.send_transac_email(send_smtp_email)
|
80 |
+
print(api_response)
|
81 |
+
print("Email Sent")
|
82 |
+
return True
|
83 |
+
except ApiException as e:
|
84 |
+
print("Can't send email")
|
85 |
+
print("Exception when calling SMTPApi->send_transac_email: %s\n" % e)
|
86 |
+
return False
|
87 |
+
|
88 |
+
def post_blogpost(access_key):
|
89 |
+
if access_key != os.getenv('cat_ids_api_key'):
|
90 |
+
return False
|
91 |
data = fetch_paper_data_with_category(cat_ids_api_key)
|
|
|
|
|
92 |
pdata = paper_data(data)
|
93 |
+
send_email(pdata)
|
94 |
+
return pdata
|