Spaces:
Sleeping
Sleeping
#!/usr/bin/env python3 | |
# -*- coding: utf-8 -*- | |
import requests | |
import gradio as gr | |
def get_bibtext_from_title(title, retry_times=100): | |
# URL编码查询参数 | |
encoded_query = requests.utils.quote(title) | |
print(f'query: {encoded_query}') | |
# 假设API的endpoint为'https://api.example.com/search' | |
url = f"https://api.semanticscholar.org/graph/v1/paper/search?query={encoded_query}&offset=0&limit=3&fields=title,citationStyles" | |
for i in range(retry_times): | |
# 发送GET请求 | |
response = requests.get(url) | |
# 检查请求是否成功 | |
if response.status_code == 200: | |
# 如果请求成功,返回JSON格式的数据 | |
try: | |
data = response.json()['data'] | |
bibtex = data[0]['citationStyles']['bibtex'] | |
return bibtex | |
except: | |
msg = f"Failed to parse response: {response.json()}" | |
return msg | |
elif response.status_code == 429: | |
print(f'retry {i} times') | |
continue | |
else: | |
# 如果请求失败,打印错误信息 | |
msg = "Failed to retrieve data: {response.json()}" | |
return msg | |
def process_text(input_text): | |
titles = input_text.split('\n') | |
# 在这个例子中,我们仅仅将输入的文本原样返回。 | |
# 你可以在这个函数中加入任何你想要的文本处理逻辑。 | |
output = [] | |
for title in titles: | |
if not title: | |
continue | |
bibtex = get_bibtext_from_title(title) | |
print(bibtex) | |
if bibtex is not None: | |
output.append(bibtex) | |
else: | |
output.append("Failed to process: " + title + '\n') | |
return '\n'.join(output) | |
# 创建Gradio界面 | |
iface = gr.Interface( | |
fn=process_text, # 要调用的处理函数 | |
inputs=gr.Textbox(lines=10, placeholder="请在此输入论文标题..."), # 输入组件:文本框,设置多行输入 | |
outputs=gr.Textbox(lines=20, placeholder="输出BibTex信息将会在此显示..."), # 输出组件:文本框,设置多行输出 | |
) | |
if __name__ == "__main__": | |
# 启动界面 | |
iface.launch() | |