LingEval / app.py
research14's picture
test
2baca0d
raw
history blame
6.45 kB
import gradio as gr
from transformers import AutoTokenizer, AutoModelForCausalLM
import time
import os
import openai
import json
import re
import io
import IPython.display
from PIL import Image
import base64
import requests, json
requests.adapters.DEFAULT_TIMEOUT = 60
# Load the Vicuna 7B v1.3 LMSys model and tokenizer
model_name = "lmsys/vicuna-7b-v1.3"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(model_name)
template_single = '''Please output any <{}> in the following sentence one per line without any additional text: "{}"'''
#API Keys
os.environ['OPENAI_API_TOKEN'] = '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'
openai.api_key = os.environ['OPENAI_API_TOKEN']
def chat(system_prompt, user_prompt, model = 'gpt-3.5-turbo', temperature = 0, verbose = False):
''' Normal call of OpenAI API '''
response = openai.ChatCompletion.create(
temperature = temperature,
model=model,
messages=[
{"role": "system", "content": system_prompt},
{"role": "user", "content": user_prompt}
])
res = response['choices'][0]['message']['content']
if verbose:
print('System prompt:', system_prompt)
print('User prompt:', user_prompt)
print('GPT response:', res)
return res
def format_chat_prompt(message, chat_history, max_convo_length):
prompt = ""
for turn in chat_history[-max_convo_length:]:
user_message, bot_message = turn
prompt = f"{prompt}\nUser: {user_message}\nAssistant: {bot_message}"
prompt = f"{prompt}\nUser: {message}\nAssistant:"
return prompt
def respond_gpt(tab_name, message, chat_history, max_convo_length = 10):
formatted_prompt = format_chat_prompt(message, chat_history, max_convo_length)
print('Prompt + Context:')
print(formatted_prompt)
bot_message = chat(system_prompt = f'''Generate the output only for the assistant. Please output any <{tab_name}> in the following sentence one per line without any additional text.''',
user_prompt = formatted_prompt)
chat_history.append((message, bot_message))
return "", chat_history
def respond(message, chat_history):
input_ids = tokenizer.encode(message, return_tensors="pt")
output_ids = model.generate(input_ids, max_length=50, num_beams=5, no_repeat_ngram_size=2)
bot_message = tokenizer.decode(output_ids[0], skip_special_tokens=True)
chat_history.append((message, bot_message))
time.sleep(2)
return "", chat_history
def interface(tab_name):
gr.Markdown(" Description ")
textbox_prompt = gr.Textbox(show_label=False, placeholder="Write a prompt and press enter")
api_key = gr.Textbox(label="Open AI Key", placeholder="Enter your Openai key here", type="password")
btn = gr.Button("Submit")
prompt = template_single.format(tab_name, textbox_prompt)
gr.Markdown("Strategy 1 QA-Based Prompting")
with gr.Row():
vicuna_S1_chatbot = gr.Chatbot(label="vicuna-7b")
llama_S1_chatbot = gr.Chatbot(label="llama-7b")
gpt_S1_chatbot = gr.Chatbot(label="gpt-3.5")
clear = gr.ClearButton(components=[textbox_prompt, vicuna_S1_chatbot])
gr.Markdown("Strategy 2 Instruction-Based Prompting")
with gr.Row():
vicuna_S2_chatbot = gr.Chatbot(label="vicuna-7b")
llama_S2_chatbot = gr.Chatbot(label="llama-7b")
gpt_S2_chatbot = gr.Chatbot(label="gpt-3.5")
clear = gr.ClearButton(components=[textbox_prompt, vicuna_S2_chatbot])
gr.Markdown("Strategy 3 Structured Prompting")
with gr.Row():
vicuna_S3_chatbot = gr.Chatbot(label="vicuna-7b")
llama_S3_chatbot = gr.Chatbot(label="llama-7b")
gpt_S3_chatbot = gr.Chatbot(label="gpt-3.5")
clear = gr.ClearButton(components=[textbox_prompt, vicuna_S3_chatbot])
textbox_prompt.submit(respond, inputs=[textbox_prompt, vicuna_S1_chatbot], outputs=[textbox_prompt, vicuna_S1_chatbot])
textbox_prompt.submit(respond, inputs=[textbox_prompt, vicuna_S2_chatbot], outputs=[textbox_prompt, vicuna_S2_chatbot])
textbox_prompt.submit(respond, inputs=[textbox_prompt, vicuna_S3_chatbot], outputs=[textbox_prompt, vicuna_S3_chatbot])
btn.click(respond_gpt, inputs=[tab_name, textbox_prompt, gpt_S1_chatbot], outputs=[tab_name, textbox_prompt, gpt_S1_chatbot])
with gr.Blocks() as demo:
gr.Markdown("# LLM Evaluator With Linguistic Scrutiny")
with gr.Tab("Noun"):
interface("Noun")
with gr.Tab("Determiner"):
gr.Markdown(" Description ")
prompt_CHUNK = gr.Textbox(show_label=False, placeholder="Write a prompt and press enter")
gr.Markdown("Strategy 1 QA")
with gr.Row():
vicuna_S1_chatbot_CHUNK = gr.Chatbot(label="vicuna-7b")
llama_S1_chatbot_CHUNK = gr.Chatbot(label="llama-7b")
gpt_S1_chatbot_CHUNK = gr.Chatbot(label="gpt-3.5")
clear = gr.ClearButton([prompt_CHUNK, vicuna_S1_chatbot_CHUNK])
gr.Markdown("Strategy 2 Instruction")
with gr.Row():
vicuna_S2_chatbot_CHUNK = gr.Chatbot(label="vicuna-7b")
llama_S2_chatbot_CHUNK = gr.Chatbot(label="llama-7b")
gpt_S2_chatbot_CHUNK = gr.Chatbot(label="gpt-3.5")
clear = gr.ClearButton([prompt_CHUNK, vicuna_S2_chatbot_CHUNK])
gr.Markdown("Strategy 3 Structured Prompting")
with gr.Row():
vicuna_S3_chatbot_CHUNK = gr.Chatbot(label="vicuna-7b")
llama_S3_chatbot_CHUNK = gr.Chatbot(label="llama-7b")
gpt_S3_chatbot_CHUNK = gr.Chatbot(label="gpt-3.5")
clear = gr.ClearButton([prompt_CHUNK, vicuna_S3_chatbot_CHUNK])
with gr.Tab("Noun phrase"):
interface("Noun phrase")
with gr.Tab("Verb phrase"):
interface("Verb phrase")
with gr.Tab("Dependent clause"):
interface("Dependent clause")
with gr.Tab("T-units"):
interface("T-units")
prompt_CHUNK.submit(respond, [prompt_CHUNK, vicuna_S1_chatbot_CHUNK], [prompt_CHUNK, vicuna_S1_chatbot_CHUNK])
prompt_CHUNK.submit(respond, [prompt_CHUNK, vicuna_S2_chatbot_CHUNK], [prompt_CHUNK, vicuna_S2_chatbot_CHUNK])
prompt_CHUNK.submit(respond, [prompt_CHUNK, vicuna_S3_chatbot_CHUNK], [prompt_CHUNK, vicuna_S3_chatbot_CHUNK])
demo.launch()