tammy-v2 / app.py
steve7909's picture
clean up
58ee241
import gradio as gr
from dotenv import load_dotenv
import os
import pandas as pd
import uuid
from datetime import datetime
from openai import OpenAI
# Load environment variables from .env file
load_dotenv()
use_local_llm = False
sentences_URL = "https://docs.google.com/spreadsheets/d/1w_MHR9coQQ7egMWbqMP8HkEysr31gKnqH2ysBzjQYfk/edit#gid=1183579691"
csv_sentence_URL = sentences_URL.replace('/edit#gid=', '/export?format=csv&gid=')
def get_sheets_sentences():
try:
csv_sheets_sentences_df = pd.read_csv(csv_sentence_URL)
print("### Successfully read CSV file from sheets")
except:
print("### Error reading CSV file from sheets")
return None
return csv_sheets_sentences_df
# Define a function to set the client based on the selection
def set_client(selection):
global use_local_llm
if selection == "Llama3":
import ollama
client = ollama.Client(host='http://10.236.173.45:11434')
use_local_llm = True
print("Using Llama3")
gr.Info(f"Using {selection}. Type in the chatbox to start.")
print(client.chat( # prime the model
model='llama3:70b',
messages=[{"role": "system", "content": "Get ready."}],
keep_alive= -1, # to prevent reloading model
options = {
'num_predict':1
}
))
print("### Llama3 model loaded")
else:
client = OpenAI(
api_key=os.getenv('OPENAI_API_KEY')
)
#OPEN_AI_KEY = ""
#client = OpenAI(api_key=OPEN_AI_KEY) # gross
use_local_llm = False
print("Using OpenAI")
gr.Info(f"Using {selection}. Type in the chatbox to start.")
return client
# Initialise the client (default to OpenAI)
client = set_client("OpenAI")
#file_path = 'anki_japanese_english_pairs.csv'
def get_sentence_pair(level):
# Load the CSV file
#file_path = 'GPT generated Japanese English sentence pairs - Sheet2.csv'
#df = pd.read_csv(file_path)
df = get_sheets_sentences()
print(df.head())
if level == None:
level = 'Easy'
print("### Level not found - default to Easy")
# Filter the DataFrame based on the desired level
filtered_df = df[df['Difficulty'] == level]
# If the filtered DataFrame is empty, return None
if filtered_df.empty:
print("### No sentences found for this level")
return None
print(filtered_df.head())
# Select a random row from the filtered DataFrame
random_row = filtered_df.sample(1)
# Extract the Japanese and English sentences
japanese_sentence = str(random_row.iloc[0, 0])
english_sentence = str(random_row.iloc[0, 1])
print(f"### Level: {level}")
print(f"### Japanese sentence: {japanese_sentence}")
print(f"### English sentence: {english_sentence}")
return (japanese_sentence, english_sentence)
def generate_system_prompt(chat_id, japanese_sentence, english_sentence):
# removed_from_system_prompt = f'''
# - Do not respond in Japanese - always respond in English even if the student uses Japanese with you.
# '''
system_prompt = f'''
**Translation Training Session**
**Feedback form:**
[Feedback Form](https://docs.google.com/forms/d/e/1FAIpQLSdqllTmXz8tEGsXSQnX1dSxbOTHxsAeBLepdDYj8DNSTYautw/viewform?usp=pp_url&entry.1679182700={chat_id})
You are an assistant to help with Japanese to English translation practice.
Help students enhance their translation skills.
**Guidelines:**
- Use hints to improve the translation iteratively.
- Do not give the correct translation (model answer) directly. Let the student work it out.
- Provide your feedback as a list where possible.
- Where the translation is correct, don't ask for another attempt. Translations are correct where they are grammatically accurate and convey the same meaning as the model answer.
- When the translation is correct, always provide the user with the feedback form.
- Where possible make any corrections bold.
- Always explain any corrections you make clearly and concisely.
- Don't say you are making a correction if you are not changing anything about the provided translation.
- When giving hints, don't reveal the correct translation and don't repeat the same hint.
- When asked about the words, give the translation for each word individually, not the full sentence translation.
- Use Japanese quotes for Japanese text. I.e. γ€Œε•ι‘Œγ€.
- Don't ask whether you should provide the feedback form, just provide it when the translation is correct.
- Do not ask if the student would like the feedback form, just provide it.
**Japanese Sentence to Translate:**
"{japanese_sentence}"
**English Sentence model answer:**
"{english_sentence}"
**Execute the following tasks:**
1. Welcome the student. Ask the student to translate the Japanese Sentence to English. Show the Japanese sentence to the student.
2. Suggest simple corrections (i.e., spelling, grammar, and punctuation). Where relevant, provide hints to improve the translation.
3. If the translation is correct or or there are only minor errors, go to step 4. If not, ask for another translation attempt for the same sentence until the translation is correct (or close).
4. When the translation is correct or the student gives up, provide the user with the feedback form (to get their thoughts on the chat). It is very important to show this form to the student.
**Feedback form:**
[Feedback Form](https://docs.google.com/forms/d/e/1FAIpQLSdqllTmXz8tEGsXSQnX1dSxbOTHxsAeBLepdDYj8DNSTYautw/viewform?usp=pp_url&entry.1679182700={chat_id})
'''
return system_prompt
def print_chat(openai_format):
for message in openai_format:
print(f"{message['role'].capitalize()}: {message['content']}")
def predict(message, history, chat_id, sentence_pair, level='Easy'):
print("### initial predict chat_id:", chat_id)
print("history length", len(history))
print("### sentence_pair:", sentence_pair)
# if not chat_id:
# chat_id = str(uuid.uuid4())[:8]
if not sentence_pair:
print("### Sentence not found - getting new sentence pair")
japanese_sentence, english_sentence = get_sentence_pair(level=level)
else:
japanese_sentence, english_sentence = sentence_pair[0], sentence_pair[1]
history_openai_format = [{"role": "system", "content": generate_system_prompt(chat_id, japanese_sentence, english_sentence)}]
for human, assistant in history:#[1:]:
history_openai_format.append({"role": "user", "content": human })
history_openai_format.append({"role": "assistant", "content":assistant})
history_openai_format.append({"role": "user", "content": message})
if use_local_llm:
stream = client.chat(
model='llama3:70b',
messages=history_openai_format,
stream=True,
keep_alive= -1, # to prevent reloading model
options = {
#'temperature': 1.5, # very creative
'temperature': 0.2 # very conservative (good for correct syntax)
}
)
partial_message = ""
response_text = ""
for chunk in stream:
response_text += chunk['message']['content'] # steve added for full response text for export
partial_message = partial_message + chunk['message']['content']
yield partial_message
else:
response = client.chat.completions.create(
model='gpt-3.5-turbo',
messages= history_openai_format,
temperature=0.2,
stream=True
)
partial_message = ""
response_text = ""
for chunk in response:
if chunk.choices[0].delta.content is not None:
response_text += chunk.choices[0].delta.content # steve added for full response text for export
partial_message = partial_message + chunk.choices[0].delta.content
yield partial_message
# after display to user, for export and print
history_openai_format.append({"role": "assistant", "content": response_text})
#print("### final chat_id:", chat_id)
export_conversation(history_openai_format, chat_id)
print_chat(history_openai_format)
#return chat_id
css = """
h1 {
text-align: center;
display: block;
}
"""
def generate_unique_id():
return str(uuid.uuid4())[:8]
# TODO: Add LLM type to messages; add difficulty level to messages
def export_conversation(history_openai_format, chat_id):
export_file_name = f"chat_{chat_id}.txt"
with open(export_file_name, "a") as file:
if file.tell() == 0:
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
file.write(f"Chat started: {chat_id} {timestamp}\n\n")
for message in history_openai_format:
#timestamp = datetime.now().strftime("%H:%M:%S")
#file.write(f"{timestamp}\t{message['role'].capitalize()}: {message['content']}\n")
file.write(f"{message['role'].capitalize()}: {message['content']}\n")
def reset(input):
return [], []
with gr.Blocks(css=css) as app:
gr.Markdown("""# <center><font size=8>{}</center>""".format("Hi, it's Tammy! Say hi to start."))
with gr.Row():
with gr.Column(scale=1):
gr.Markdown("""## Instructions""")
gr.Markdown("""
**Welcome to Tammy!**
- Type your message in the textbox and press enter or Submit to send.
- Click Retry if the chatbot is stuck or the response is a little strange.
- Your chats are recorded for quality assurance and training purposes. Behave.
""")
difficulty_level = gr.Dropdown(choices=["Easy", "Intermediate", "Advanced"], value="Easy", label="Difficulty Level", interactive=True)
# llm_type = gr.Dropdown(choices=["Llama3", "OpenAI"], value="OpenAI", label="Choose LLM Type")
# Define a function to handle changes in dropdown
# def update_client(llm):
# global client
# client = set_client(llm)
# Button to apply the change
# apply_btn = gr.Button("Apply")
# apply_btn.click(fn=update_client, inputs=llm_type, outputs=None)
# gr.Markdown(f"""[Click here](https://docs.google.com/forms/d/e/1FAIpQLSdqllTmXz8tEGsXSQnX1dSxbOTHxsAeBLepdDYj8DNSTYautw/viewform?usp=pp_url&entry.1679182700={chat_id})
# """)
with gr.Column(scale=3):
bot = gr.Chatbot(render=False, height=550)
sentence_pair_state = gr.State(get_sentence_pair(level=difficulty_level.value))
chat_id = gr.State(generate_unique_id)
#print("### ui chat_id:", chat_id)
chat = gr.ChatInterface(
predict,
chatbot=bot,
additional_inputs=[chat_id, sentence_pair_state, difficulty_level],
examples=[
["Help me start", None, None, None],
["Give me a hint", None, None, None],
["I need more help", None, None, None],
["What do you mean?", None, None, None],
["What do the words mean?", None, None, None],
["Let's stop now", None, None, None]
],
)
difficulty_level.input(fn=reset, inputs=difficulty_level, outputs=[bot, chat.chatbot_state])
def update_sentence_pair(level):
global sentence_pair_state
gr.Info(f"New {level} sentence selected. Type in the chatbox to start.")
print("### updating level:", level)
sentence_pair = get_sentence_pair(level)
print("### new sentence pair:", sentence_pair)
sentence_pair_state = gr.State(sentence_pair)
return sentence_pair
# def clear_chat_history(level):
# bot.clear()
# gr.Info(f"New {level} sentence selected. Type in the chatbox to start.")
# print("### updating level:", level)
# sentence_pair.data = get_sentence_pair(level=level)
#difficulty_level.change(clear_chat_history, inputs=difficulty_level, outputs=sentence_pair)
difficulty_level.change(update_sentence_pair, inputs=difficulty_level, outputs=sentence_pair_state)
app.launch()