Spaces:
Sleeping
Sleeping
import gradio as gr | |
import json | |
import os | |
import sys | |
import csv | |
import requests | |
import json | |
import pandas as pd | |
import concurrent.futures | |
from tqdm import tqdm | |
import shutil | |
import numpy as np | |
import seaborn as sns | |
from matplotlib import pyplot as plt | |
import pickle | |
from scipy.stats import percentileofscore | |
mean_citation_list = [] | |
# Open the file and read the content in a list | |
with open('mean_citation_list.txt', 'r') as filehandle: | |
for line in filehandle: | |
temp = float(line[:-1]) | |
mean_citation_list.append(temp) | |
# # Read list to memory | |
# def read_list(): | |
# # for reading also binary mode is important | |
# with open('mean_aoc_all_papers.pkl', 'rb') as fp: | |
# n_list = pickle.load(fp) | |
# return n_list | |
# mean_citation_list = read_list() | |
def generate_plot_maoc(input_maoc): | |
plt.clf() | |
sns.set(font_scale = 8) | |
sns.set(rc={'figure.figsize':(10,6)}) | |
sns.set_style(style='whitegrid') | |
ax = sns.histplot(mean_citation_list, bins=100, kde=True, color='skyblue') | |
kdeline = ax.lines[0] | |
xs = kdeline.get_xdata() | |
ys = kdeline.get_ydata() | |
interpolated_y_maoc = np.interp(input_maoc, kdeline.get_xdata(), kdeline.get_ydata()) | |
ax.scatter(input_maoc, interpolated_y_maoc,c='r', marker='*',linewidths=5, zorder=2) | |
ax.vlines(input_maoc, 0, interpolated_y_maoc, color='tomato', ls='--', lw=2) | |
epsilon = 0.3 | |
ax.text(input_maoc + epsilon, interpolated_y_maoc + epsilon, 'Your paper', {'color': '#DC143C', 'fontsize': 13}) | |
ax.set_xlabel("mean Age of Citation(mAoC)",fontsize=15) | |
ax.set_ylabel("Number of papers",fontsize=15) | |
ax.tick_params(axis='both', which='major', labelsize=12) | |
percentile_of_input_maoc = percentileofscore(mean_citation_list, input_maoc) | |
percentile_of_input_maoc = round(percentile_of_input_maoc, 2) | |
title = "The mAoC of your paper is at " + r"$\bf{" + str(percentile_of_input_maoc) + "}$" + "-th percentile of all the papers \n in our database (papers published until 2021 years)" | |
plt.title(title, fontsize=12) | |
return plt | |
# sent a request | |
def request_to_respose(request_url): | |
request_response = requests.get(request_url, headers={'x-api-key': 'qZWKkOKyzP5g9fgjyMmBt1MN2NTC6aT61UklAiyw'}) | |
return request_response | |
def return_clear(): | |
return None, None, None, None, None | |
def compute_output(ssid_paper_id): | |
output_num_ref = 0 | |
output_maoc = 0 | |
oldest_paper_list = "" | |
request_url = f'https://api.semanticscholar.org/graph/v1/paper/{ssid_paper_id}?fields=references,title,venue,year' | |
r = request_to_respose(request_url) | |
if r.status_code == 200: # if successful request | |
s2_ref_paper_keys = [reference_paper_tuple['paperId'] for reference_paper_tuple in r.json()['references']] | |
filtered_s2_ref_paper_keys = [s2_ref_paper_key for s2_ref_paper_key in s2_ref_paper_keys if s2_ref_paper_key is not None] | |
total_references = len(s2_ref_paper_keys) | |
none_references = (len(s2_ref_paper_keys) - len(filtered_s2_ref_paper_keys)) | |
s2_ref_paper_keys = filtered_s2_ref_paper_keys | |
# print(r.json()) | |
s2_paper_key, title, venue, year = r.json()['paperId'], r.json()['title'], r.json()['venue'], r.json()['year'] | |
reference_year_list = [] | |
reference_title_list = [] | |
for ref_paper_key in s2_ref_paper_keys: | |
request_url_ref = f'https://api.semanticscholar.org/graph/v1/paper/{ref_paper_key}?fields=references,title,venue,year' | |
r_ref = request_to_respose(request_url_ref) | |
if r_ref.status_code == 200: | |
s2_paper_key_ref, title_ref, venue_ref, year_ref = r_ref.json()['paperId'], r_ref.json()['title'], r_ref.json()['venue'], r_ref.json()['year'] | |
reference_year_list.append(year_ref) | |
reference_title_list.append(title_ref) | |
# print(f'Number of references for which we got the year = {len(reference_year_list)}') | |
output_num_ref = len(reference_year_list) | |
aoc_list = [year - year_ref for year_ref in reference_year_list] | |
output_maoc = sum(aoc_list)/len(aoc_list) | |
sorted_ref_title_list = [x for _,x in sorted(zip(reference_year_list,reference_title_list))] | |
sorted_ref_year_list = [x for x,_ in sorted(zip(reference_year_list,reference_title_list))] | |
text = "" | |
sorted_ref_title_list = sorted_ref_title_list[:min(len(sorted_ref_title_list), 5)] | |
sorted_ref_year_list = sorted_ref_year_list[:min(len(sorted_ref_year_list), 5)] | |
for i in range(len(sorted_ref_year_list)): | |
text += '[' + str(sorted_ref_year_list[i]) + ']' + " Title: " + sorted_ref_title_list[i] + '\n' | |
oldest_paper_list = text | |
plot_maoc = generate_plot_maoc(output_maoc) | |
# print(plot_maoc) | |
return output_num_ref, output_maoc, oldest_paper_list, gr.update(value=plot_maoc) | |
with gr.Blocks(theme=gr.themes.Soft()) as demo: | |
with gr.Row(): | |
gr.Markdown( | |
""" | |
<center><h2>Citational Amnesia</h2></center> | |
<br> | |
<center>Demo to predict the number of references, mean age of citation(mAoC), and comparison of mAoC with all the papers in the ACL Anthology. </center> | |
<center>Kindly enter the Semantic Scholar ID(SSID) of the paper in the box and click <i>"Generate"</i> </center> | |
<br> | |
<center><h3>Retrieving SSID</h3> For paper : <a href="https://www.semanticscholar.org/paper/Geographic-Citation-Gaps-in-NLP-Research-Rungta-Singh/6f8ab0fa15c87cee55b2ca5b8877e6784887ddbf">https://www.semanticscholar.org/paper/Geographic-Citation-Gaps-in-NLP-Research-Rungta-Singh/6f8ab0fa15c87cee55b2ca5b8877e6784887ddbf</a></center> | |
<center>SSID is : <b>6f8ab0fa15c87cee55b2ca5b8877e6784887ddbf</b></center> | |
<center> <i>Note: Currently we only support SSID as the input format</i> </center> | |
""" | |
) | |
with gr.Row(): | |
ss_paper_id = gr.Textbox(label='Semantic Scholar ID',placeholder="Enter the Semantic Scholar ID here and press enter...", lines=1) | |
with gr.Row(): | |
submit_btn = gr.Button("Generate") | |
with gr.Row(): | |
num_ref = gr.Textbox(label="Number of references") | |
mAoc = gr.Textbox(label="Mean AoC") | |
with gr.Row(): | |
oldest_paper_list = gr.Textbox(label="Top 5 oldest papers cited:",lines=5) | |
with gr.Row(): | |
mAocPlot = gr.Plot(label="Plot") | |
with gr.Row(): | |
clear_btn = gr.Button("Clear") | |
submit_btn.click(fn = compute_output, inputs = [ss_paper_id], outputs = [num_ref, mAoc, oldest_paper_list, mAocPlot]) | |
# clear_btn.click(lambda: None, None, None, queue=False) | |
clear_btn.click(fn = return_clear, inputs=[], outputs=[ss_paper_id, num_ref, mAoc, oldest_paper_list, mAocPlot]) | |
demo.queue(concurrency_count=3) | |
demo.launch() | |
# with gr.Blocks() as demo: | |
# ss_paper_id = gr.Textbox(label='Semantic Scholar ID',placeholder="Enter the Semantic Scholar ID here and press enter...", lines=1) | |
# submit_btn = gr.Button("Generate") | |
# with gr.Row(): | |
# num_ref = gr.Textbox(label="Number of references") | |
# mAoc = gr.Textbox(label="Mean AoC") | |
# with gr.Row(): | |
# oldest_paper_list = gr.Textbox(label="Top 5 oldest papers cited:",lines=5) | |
# with gr.Row(): | |
# mAocPlot = gr.Plot(label="Plot") | |
# clear_btn = gr.Button("Clear") | |
# submit_btn.click(fn = compute_output, inputs = [ss_paper_id], outputs = [num_ref, mAoc, oldest_paper_list, mAocPlot]) | |
# # clear_btn.click(lambda: None, None, None, queue=False) | |
# clear_btn.click(fn = return_clear, inputs=[], outputs=[ss_paper_id, num_ref, mAoc, oldest_paper_list, mAocPlot]) | |
# demo.launch() | |
# import openai | |
# import gradio | |
# openai.api_key = "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" | |
# messages = [{"role": "system", "content": "Anhub Online Education Tutor for Any Subjects:"}] | |
# def CustomChatGPT(user_input): | |
# messages.append({"role": "user", "content": user_input}) | |
# response = openai.ChatCompletion.create( | |
# model = "gpt-3.5-turbo", | |
# messages = messages | |
# ) | |
# ChatGPT_reply = response["choices"][0]["message"]["content"] | |
# messages.append({"role": "assistant", "content": ChatGPT_reply}) | |
# return ChatGPT_reply | |
# demo = gradio.Interface(fn=CustomChatGPT, inputs = "text", outputs = "text", title = "Anhub Metaverse Education Online Tutor for Any Subjects and any Languages @ 24 x 7:") | |
# demo.launch() |