Spaces:
Sleeping
Sleeping
import gradio as gr | |
from openai import OpenAI | |
import json | |
import os | |
env_key = os.environ.get("api_key") | |
base_url = os.environ.get("mistrial_api") | |
client = OpenAI( | |
api_key=env_key, | |
base_url=base_url | |
) | |
def test_api(): | |
models = client.models.list() | |
return str(models.model_dump()) | |
def generate(user_prompt,system_prompt, temperature, top_p,api_key): | |
if api_key != env_key: | |
return { | |
"error": "Invalid API key" | |
} | |
chat_completion = client.chat.completions.create( | |
messages=[ | |
{"role": "system", "content": system_prompt}, | |
{"role": "user", "content": user_prompt}, | |
], | |
seed=42, | |
model="mistral", | |
n=3, | |
temperature=temperature, | |
top_p=top_p, | |
) | |
return json.loads(chat_completion.choices[0].message.content) | |
def review_paper(paper_abstract, temperature, top_p,api_key): | |
system_prompt = "###System prompt:\nYou are a helpful, professional reviewer. You are reviewing a scientific paper. You should provide at least 2 contribution bullets. You should provide at least 2 strengths and 2 weaknesses of the paper. You should provide at least 2 suggestions to improve the paper. You should provide at least 3 questions to the authors.\n###OutputFomrat:\nYou should return your answer in JSON format. For example, {'contribution': ['This paper proposes a new method for ...', 'This paper proposes a new method for ...'], 'strengths': ['The paper is well written.', 'The paper is well written.'], 'weaknesses': ['The paper is not well written.', 'The paper is not well written.'], 'suggestions': ['The authors should ...', 'The authors should ...'], 'questions': ['Why do you ...?', 'Why do you ...?']}" | |
user_prompt = "###Paper abstract:\n" + paper_abstract | |
result = generate(user_prompt, system_prompt, temperature, top_p,api_key) | |
return json.dumps(result, indent=4, ensure_ascii=False) | |
iface = gr.Interface(fn=review_paper, inputs=["text", gr.Slider(0.0, 1.0), gr.Slider(0.0, 1.0), "text"], | |
outputs="text", title="Review Paper", description="This is a tool to help you review a paper.") | |
iface.launch() | |