Raghavan1988's picture
Update app.py
b456778
import json
import os
import shutil
import requests
import gradio as gr
from huggingface_hub import Repository, InferenceClient
HF_TOKEN = os.environ.get("HF_TOKEN", None)
API_URL = "https://api-inference.huggingface.co/models/tiiuae/falcon-180B-chat"
BOT_NAME = "Falcon"
STOP_SEQUENCES = ["\nUser:", "<|endoftext|>", " User:", "###"]
EXAMPLES = [["climate change"], ["2308.15699"], ["hallucination"], ["2308.00205"], ["large language model"], ["2308.05204"], ["2308.10873"], ["2308.06355"],["2308.01684"],["2308.00352"],["2308.07773"]]
client = InferenceClient(
API_URL,
headers={"Authorization": f"Bearer {HF_TOKEN}"},
)
id_dict = {}
for i in range(0,4):
fname = "arxiv_2023_" + str(i)
with open(fname, "r") as f:
for line in f:
D = json.loads(line)
id_dict[D['id']] = D
def format_prompt_summarize(message, history, system_prompt, keyword):
prompt = ""
prompt += "System: You are scholarly RESEARCH ASSISTANT who can read the ARXIV scholarly article.\n"
prompt += "User: READ ALL THE TITLEs and ABSTRACTs of various article below\n"
prompt += "Generate a SUMMARY of all the articles below relevant to the research for the field of \"" + keyword + "\"\n"
prompt += "SUGGEST FIVE IMPORTANT FINDINGS or ORIGINAL CONTRIBUTIONS of OBSERVATIONs for the field of \"" + keyword + "\" that summarizes the work.\n"
prompt += "Each BULLET POINT must be be less than 15 WORDS. \n"
prompt += "Output the FIVE KEY FINDINGS as BULLET POINTS with UNDERLINE OR BOLDEN KEY PHRASES.\n"
prompt += "Propose ONE CREATIVE ACTIONABLE IDEA for FUTURE extension of the RESEARCH\n. You MUST output the CREATIVE IDEA with a BULB OR IDEA OR THINKING emoji.\n"
prompt += "Output ONE CREATIVE IDEA for FUTURE extension with a RANDOM emoji\n"
prompt += "Choose an UNRELATED or ORTHOGONAL field where the FINDINGS of the article can be applied.\n"
prompt += "In a new line, OUTPUT ONE CRAZY IDEA in 20 WORDS how the KEY FINDINGS of RESEARCH article can be applied in an ORTHOGONAL or UNRELATED FIELD with a CRAZY IDEA emoji \n"
prompt += message + "\n"
mock_prompt = ""
if system_prompt == "":
mock_prompt += f"System: {system_prompt}\n"
for user_prompt, bot_response in history:
mock_prompt += f"User: {user_prompt}\n"
mock_prompt += f"Falcon: {bot_response}\n" # Response already contains "Falcon: "
mock_prompt += f"""User: {message}
Falcon:"""
return prompt
def format_prompt(message, history, system_prompt):
prompt = ""
prompt += "System: You are scholarly RESEARCH ASSISTANT who can read the ARXIV scholarly article.\n"
prompt += "READ THE TITLE and ABSTRACT of the article below\n"
prompt += "After understanding the ABSTRACT, SUGGEST 4 IMPORTANT FINDINGS or ORIGINAL CONTRIBUTIONS of OBSERVATIONs that summarizes the work.\n"
prompt += "Each BULLET POINT must be be less than 15 WORDS. \n"
prompt += "Output the FOUR KEY FINDINGS as BULLET POINTS with UNDERLINE OR BOLDEN KEY PHRASES.\n"
prompt += "Propose ONE CREATIVE ACTIONABLE IDEA for FUTURE extension of the RESEARCH\n. You MUST output the CREATIVE IDEA with a BULB OR IDEA OR THINKING emoji.\n"
prompt += "Output ONE CREATIVE IDEA for FUTURE extension with a RANDOM emoji\n"
prompt += "Choose an UNRELATED or ORTHOGONAL field where the FINDINGS of the article can be applied.\n"
prompt += "In a new line, OUTPUT ONE CRAZY IDEA in 20 WORDS how the KEY FINDINGS of RESEARCH article can be applied in an ORTHOGONAL or UNRELATED FIELD with a CRAZY IDEA emoji \n"
prompt += "User:" + message + "\n"
mock_prompt = ""
if system_prompt == "":
mock_prompt += f"System: {system_prompt}\n"
for user_prompt, bot_response in history:
mock_prompt += f"User: {user_prompt}\n"
mock_prompt += f"Falcon: {bot_response}\n" # Response already contains "Falcon: "
mock_prompt += f"""User: {message}
Falcon:"""
return prompt
seed = 42
def generate(
prompt, history, system_prompt="", temperature=0.9, max_new_tokens=256, top_p=0.95, repetition_penalty=1.0,
):
temperature = float(temperature)
if temperature < 1e-2:
temperature = 1e-2
top_p = float(top_p)
global seed
generate_kwargs = dict(
temperature=temperature,
max_new_tokens=max_new_tokens,
top_p=top_p,
repetition_penalty=repetition_penalty,
stop_sequences=STOP_SEQUENCES,
do_sample=True,
seed=seed,
)
seed = seed + 1
title = "INPUT ARXI ID"
abstract = ""
if prompt in id_dict:
title = id_dict[prompt]['title']
abstract = id_dict[prompt]['abstract']
prompt = f"TITLE: {title} ABSTRACT: {abstract}\n"
output = f"<b>Title: </b> {title} \n <br>"
formatted_prompt = format_prompt(prompt, history, system_prompt)
else:
keyword = prompt
counter= 0
for d in id_dict:
title = id_dict[d]['title']
abstract = id_dict[d]['abstract']
if keyword in title or keyword in abstract:
counter+=1## its a hit
prompt += "ARTICLE " + str(counter) + "\n"
prompt += f"TITLE: {title} ABSTRACT: {abstract}\n"
if counter >= 4:
break
prompt += "Keyword: " + keyword + "\n"
formatted_prompt = format_prompt_summarize(prompt, history, system_prompt, keyword)
output = "Articles related to the keyword " + keyword + "\n"
stream = client.text_generation(formatted_prompt, **generate_kwargs, stream=True, details=True, return_full_text=False)
#output = ""
for response in stream:
output += response.token.text
for stop_str in STOP_SEQUENCES:
if output.endswith(stop_str):
output = output[:-len(stop_str)]
output = output.rstrip()
yield output
yield output
return output
additional_inputs=[
gr.Textbox("", label="Optional system prompt"),
gr.Slider(
label="Temperature",
value=0.9,
minimum=0.0,
maximum=1.0,
step=0.05,
interactive=True,
info="Higher values produce more diverse outputs",
),
gr.Slider(
label="Max new tokens",
value=256,
minimum=0,
maximum=8192,
step=64,
interactive=True,
info="The maximum numbers of new tokens",
),
gr.Slider(
label="Top-p (nucleus sampling)",
value=0.90,
minimum=0.0,
maximum=1,
step=0.05,
interactive=True,
info="Higher values sample more low-probability tokens",
),
gr.Slider(
label="Repetition penalty",
value=1.2,
minimum=1.0,
maximum=2.0,
step=0.05,
interactive=True,
info="Penalize repeated tokens",
)
]
with gr.Blocks() as demo:
with gr.Row():
with gr.Column(scale=0.4):
gr.Image("better_banner.jpeg", elem_id="banner-image", show_label=False)
with gr.Column():
gr.Markdown(
"""
#
** The idea is inspired by CREATIVE WHACK PACK https://apps.apple.com/us/app/creative-whack-pack/id307306326
** ##Researchers need INSPIRATION to come up with CREATIVE IDEAS.
** ###We use Falcon 180B to
<br> - generate a <b>SUMMARY</b> of the arxiv articles (only August articles are supported)
<br> - generate a <b>CREATIVE IDEA </b> for future extension
<br> - generate a </b>CRAZY IDEA</b> for application in an orthogonal field.
This should hopefully CONNECT unrelated fields and inspire researchers to come up with CREATIVE IDEAS.
## Please input ARXIV ID or a query, see examples below (limited to 15K articles from August 2023)
➡️️ **Intended Use**: this demo is intended to showcase how LLMs can be used to generate creative ideas for future extension and application in orthogonal field.
⚠️ **Limitations**: the model can and will produce factually incorrect information, hallucinating facts and actions. As it has not undergone any advanced tuning/alignment, it can produce problematic outputs, especially if prompted to do so. Finally, this demo is limited to a session length of about 1,000 words.
"""
)
gr.ChatInterface(
generate,
examples=EXAMPLES,
additional_inputs=additional_inputs,
)
demo.queue(concurrency_count=100, api_open=False).launch(show_api=False)