Spaces:
Runtime error
Runtime error
shaocongma
commited on
Commit
·
c104543
1
Parent(s):
1de6eae
add new functions
Browse files
chat.py
ADDED
|
@@ -0,0 +1,124 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import os
|
| 3 |
+
import openai
|
| 4 |
+
from utils.references import References
|
| 5 |
+
from utils.gpt_interaction import GPTModel
|
| 6 |
+
from utils.prompts import SYSTEM
|
| 7 |
+
|
| 8 |
+
openai_key = os.getenv("OPENAI_API_KEY")
|
| 9 |
+
default_model = os.getenv("DEFAULT_MODEL")
|
| 10 |
+
openai.api_key = openai_key
|
| 11 |
+
|
| 12 |
+
contribution_system_prompt_1 = '''You are an assistant designed to propose potential contributions of a given title of the paper. Ensure follow the following instructions:
|
| 13 |
+
Instruction:
|
| 14 |
+
- Your response should follow the JSON format.
|
| 15 |
+
- Your response should have the following structure: {"contribution1": {"statement": "briefly describe what the contribution is", "reason": "reason why this contribution has not been made by other literatures"}, "contribution2": {"statement": "briefly describe what the contribution is", "reason": "reason why this contribution has not been made by other literatures"}, ...}'''
|
| 16 |
+
|
| 17 |
+
contribution_system_prompt_2 = '''You are an assistant designed to criticize the contributions of a paper. You will be provided Paper's Title, References and Contributions. Ensure follow the following instructions:
|
| 18 |
+
Instruction:
|
| 19 |
+
- Your response should follow the JSON format.
|
| 20 |
+
- Your response should have the following structure:
|
| 21 |
+
{"contribution1": {"statement": "briefly describe what the contribution is", "reason": "reason why this becomes a contribution from the user", "think":"your thought about if this is a novel contribution", "criticism": "reason why this can or cannot be a novel contribution"},
|
| 22 |
+
"contribution2": {"statement": "briefly describe what the contribution is", "reason": "reason why this becomes a contribution from the user", "think":"your thought about if this is a novel contribution", "criticism": "reason why this can or cannot be a novel contribution"}, ...}
|
| 23 |
+
- You need to carefully check if the claimed contribution has been made in the provided references, which makes the contribution not novel.
|
| 24 |
+
'''
|
| 25 |
+
|
| 26 |
+
suggestions_system_prompt = '''You are an assistant designed to help improve the novelty of a paper. You will be provided Paper's Title, References, Criticism, and Contributions. Ensure follow the following instructions:
|
| 27 |
+
Instruction:
|
| 28 |
+
- Your response should follow the JSON format.
|
| 29 |
+
- Your response should have the following structure:
|
| 30 |
+
{"title": {"suggestion": "your suggestion on the title", "new title": "your suggested title based on your suggestion", "reason": "your reason why you want to make such modification based on the references and criticism"},
|
| 31 |
+
"contributions": {"new contribution 1": {"statement": "your proposed new contribution", "reason": "why this is a novel contribution"}, "new contribution 2": {"statement": "your proposed new contribution", "reason": "why this is a novel contribution"}, ...}}
|
| 32 |
+
- Your reason should be based on the references and solve the criticism.
|
| 33 |
+
'''
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
ANNOUNCEMENT = """
|
| 37 |
+
# Paper Bot
|
| 38 |
+
|
| 39 |
+
Criticize your paper's contribution by searching related references online! This nice bot will also give you some suggestions.
|
| 40 |
+
"""
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
def reset():
|
| 44 |
+
return "", "", {}, {}, {}
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
def search_refs(title, contributions):
|
| 48 |
+
ref = References(title=title, description=contributions)
|
| 49 |
+
|
| 50 |
+
keywords, _ = llm(systems=SYSTEM["keywords"], prompts=title, return_json=True)
|
| 51 |
+
keywords = {keyword: 10 for keyword in keywords}
|
| 52 |
+
|
| 53 |
+
ref.collect_papers(keywords)
|
| 54 |
+
return ref.to_prompts(max_tokens=8192)
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
def criticize_my_idea(title, contributions, refined_contributions, suggestions):
|
| 58 |
+
if refined_contributions:
|
| 59 |
+
cont = {k: {"statement": v["statement"]} for k, v in refined_contributions.items()}
|
| 60 |
+
|
| 61 |
+
ref = References(title=title, description=f"{cont}")
|
| 62 |
+
keywords, _ = llm(systems=SYSTEM["keywords"], prompts=title, return_json=True)
|
| 63 |
+
keywords = {keyword: 10 for keyword in keywords}
|
| 64 |
+
ref.collect_papers(keywords)
|
| 65 |
+
ref_prompt = ref.to_prompts(max_tokens=4096)
|
| 66 |
+
|
| 67 |
+
prompt = f"Title: {title}\n References: {ref_prompt}\n Contributions: {cont}"
|
| 68 |
+
output, _ = llm(systems=contribution_system_prompt_2, prompts=prompt, return_json=True)
|
| 69 |
+
|
| 70 |
+
suggestions, _ = llm(systems=suggestions_system_prompt, prompts=str(output), return_json=True)
|
| 71 |
+
|
| 72 |
+
return output, ref_prompt, suggestions
|
| 73 |
+
else:
|
| 74 |
+
ref = References(title=title, description=f"{contributions}")
|
| 75 |
+
keywords, _ = llm(systems=SYSTEM["keywords"], prompts=title, return_json=True)
|
| 76 |
+
keywords = {keyword: 10 for keyword in keywords}
|
| 77 |
+
ref.collect_papers(keywords)
|
| 78 |
+
ref_prompt = ref.to_prompts(max_tokens=4096)
|
| 79 |
+
|
| 80 |
+
prompt = f"Title: {title}\n References: {ref_prompt}\n Contributions: {contributions}"
|
| 81 |
+
output, _ = llm(systems=contribution_system_prompt_1, prompts=prompt, return_json=True)
|
| 82 |
+
return output, ref_prompt, {}
|
| 83 |
+
|
| 84 |
+
def paste_title(suggestions):
|
| 85 |
+
if suggestions:
|
| 86 |
+
title = suggestions['title']['new title']
|
| 87 |
+
contributions = suggestions['contributions']
|
| 88 |
+
|
| 89 |
+
return title, contributions, {}, {}, {}
|
| 90 |
+
else:
|
| 91 |
+
return "", "", {}, {}, {}
|
| 92 |
+
|
| 93 |
+
|
| 94 |
+
|
| 95 |
+
|
| 96 |
+
|
| 97 |
+
with gr.Blocks() as demo:
|
| 98 |
+
llm = GPTModel(model=default_model)
|
| 99 |
+
gr.Markdown(ANNOUNCEMENT)
|
| 100 |
+
|
| 101 |
+
with gr.Row():
|
| 102 |
+
with gr.Column():
|
| 103 |
+
title_input = gr.Textbox(label="Title")
|
| 104 |
+
contribution_input = gr.Textbox(label="Contributions", lines=5)
|
| 105 |
+
|
| 106 |
+
with gr.Row():
|
| 107 |
+
button_reset = gr.Button("Reset")
|
| 108 |
+
button_submit = gr.Button("Submit", variant="primary")
|
| 109 |
+
|
| 110 |
+
with gr.Column(scale=1):
|
| 111 |
+
contribution_output = gr.JSON(label="Contributions")
|
| 112 |
+
suggestions_output = gr.JSON(label="Suggestions")
|
| 113 |
+
button_copy = gr.Button("Send Title and Contributions to the Left")
|
| 114 |
+
references_output = gr.JSON(label="References")
|
| 115 |
+
|
| 116 |
+
button_reset.click(fn=reset, inputs=[], outputs=[title_input, contribution_input, contribution_output, references_output, suggestions_output])
|
| 117 |
+
button_submit.click(fn=criticize_my_idea, inputs=[title_input, contribution_input, contribution_output, suggestions_output], outputs=[contribution_output, references_output, suggestions_output])
|
| 118 |
+
button_copy.click(fn=paste_title, inputs=suggestions_output, outputs=[title_input, contribution_input, contribution_output, references_output, suggestions_output])
|
| 119 |
+
# clear_button_refs.click(fn=clear_inputs_refs, inputs=[title_refs, slider_refs], outputs=[title_refs, slider_refs])
|
| 120 |
+
# submit_button_refs.click(fn=wrapped_references_generator,
|
| 121 |
+
# inputs=[title_refs, slider_refs, key], outputs=json_output)
|
| 122 |
+
|
| 123 |
+
demo.queue(concurrency_count=1, max_size=5, api_open=False)
|
| 124 |
+
demo.launch(show_error=True)
|