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 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 = 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): 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) 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( """