import gradio as gr
from llama_cpp import Llama
import os
import json
from gradio_modal import Modal
os.environ["HF_HUB_ENABLE_HF_TRANSFER"] = "1"
Imports
from huggingface_hub import hf_hub_download
from llama_cpp import Llama
Initialize the Llama model
Download the GGUF model
model_name = "johnpaulbin/qwen2.5-3b-tokipona-v4-merged-fp16-chat-Q8_0-GGUF"
model_file = "qwen2.5-3b-tokipona-v4-merged-fp16-chat-q8_0.gguf" # This is the specific model file we'll use in this example.
model_path = hf_hub_download(model_name, filename=model_file)
Instantiate model from downloaded file
llm = Llama(
model_path=model_path,
n_ctx=1024, # Context length to use
n_threads=2, # Number of CPU threads to use
n_gpu_layers=-1, # Number of model layers to offload to GPU
chat_format="chatml",
flash_attn=True
)
Initialize the conversation with a system prompt
system_prompt = "sina ilo Elelen. sina jan pona pi wile pali. sina pona tawa jan ante li wile pali pona tawa ona."
initial_messages = [{"role": "system", "content": system_prompt}]
# Update the chat history
chat_history = chat_history + [(user_message, assistant_response)]
return "", chat_history, messages
except ValueError as e:
print(f"Error: You've reached the max context length for the model. Clear the conversation to continue typing.")
code
assistant_response = "Error: You've reached the max context length for the model. Clear the conversation to continue typing."
# Append the assistant's response to the conversation history
messages = messages + [{"role": "assistant", "content": assistant_response}]
# Update the chat history
chat_history = chat_history + [(user_message, assistant_response)]
return "", chat_history, messages
Assistant
def upvote_click(chat_history, messages, dataset):
notification = "Your feedback has been submitted! Feel free to keep chatting or clear the history."
if len(messages) >= 2:
Construct the full conversation history
full_conversation = ""
for msg in messages:
if msg['role'] == 'system':
full_conversation += f"<|im_start|>system\n{msg['content']}<|im_end|>\n"
elif msg['role'] == 'user':
full_conversation += f"<|im_start|>user\n{msg['content']}<|im_end|>\n"
elif msg['role'] == 'assistant':
full_conversation += f"<|im_start|>assistant\n{msg['content']}<|im_end|>"
code
# Split the conversation into prompt and completion
prompt_parts = full_conversation.rsplit("<|im_start|>assistant", 1)
prompt = prompt_parts[0]
completion = f"<|im_start|>assistant{prompt_parts[1]}" if len(prompt_parts) > 1 else ""
# Read existing data from feedback.json
try:
with open('feedback.json', 'r') as f:
existing_data = json.load(f)
except FileNotFoundError:
existing_data = {'prompt': [], 'completion': [], 'label': []}
# Check for duplicates
duplicate = False
for p, c in zip(existing_data['prompt'], existing_data['completion']):
if p == prompt and c == completion:
duplicate = True
break
if not duplicate:
existing_data['prompt'].append(prompt)
existing_data['completion'].append(completion)
existing_data['label'].append(True)
# Save to feedback.json
with open('feedback.json', 'w') as f:
json.dump(existing_data, f)
dataset = existing_data
print("Upvoted the response! YAAY!!!")
else:
notification = "You have already submitted feedback for this conversation."
else:
notification = "No conversation to submit feedback for."
return dataset, notification
def downvote_click(chat_history, messages, dataset):
notification = "Your feedback has been submitted! Feel free to keep chatting or clear the history."
if len(messages) >= 2:
Construct the full conversation history
full_conversation = ""
for msg in messages:
if msg['role'] == 'system':
full_conversation += f"<|im_start|>system\n{msg['content']}<|im_end|>\n"
elif msg['role'] == 'user':
full_conversation += f"<|im_start|>user\n{msg['content']}<|im_end|>\n"
elif msg['role'] == 'assistant':
full_conversation += f"<|im_start|>assistant\n{msg['content']}<|im_end|>"
code
# Split the conversation into prompt and completion
prompt_parts = full_conversation.rsplit("<|im_start|>assistant", 1)
prompt = prompt_parts[0]
completion = f"<|im_start|>assistant{prompt_parts[1]}" if len(prompt_parts) > 1 else ""
# Read existing data from feedback.json
try:
with open('feedback.json', 'r') as f:
existing_data = json.load(f)
except FileNotFoundError:
existing_data = {'prompt': [], 'completion': [], 'label': []}
# Check for duplicates
duplicate = False
for p, c in zip(existing_data['prompt'], existing_data['completion']):
if p == prompt and c == completion:
duplicate = True
break
if not duplicate:
existing_data['prompt'].append(prompt)
existing_data['completion'].append(completion)
existing_data['label'].append(False)
# Save to feedback.json
with open('feedback.json', 'w') as f:
json.dump(existing_data, f)
dataset = existing_data
print("Downvoted the response!")
else:
notification = "You have already submitted feedback for this conversation."
else:
notification = "No conversation to submit feedback for."
return dataset, notification
def calculate_overall_score(ratings):
weights = {
'accuracy': 0.3,
'fluency': 0.2,
'helpfulness': 0.3,
'appropriateness': 0.2
}
overall_score = sum(ratings[category] * weight for category, weight in weights.items())
return overall_score
def submit_feedback(chat_history, messages, dataset, upvote, accuracy, fluency, helpfulness, appropriateness, comment):
notification = "Your feedback has been submitted! Feel free to keep chatting or clear the history."
if len(messages) >= 2:
full_conversation = ""
for msg in messages:
if msg['role'] == 'system':
full_conversation += f"<|im_start|>system\n{msg['content']}<|im_end|>\n"
elif msg['role'] == 'user':
full_conversation += f"<|im_start|>user\n{msg['content']}<|im_end|>\n"
elif msg['role'] == 'assistant':
full_conversation += f"<|im_start|>assistant\n{msg['content']}<|im_end|>"
code
prompt_parts = full_conversation.rsplit("<|im_start|>assistant", 1)
prompt = prompt_parts[0]
completion = f"<|im_start|>assistant{prompt_parts[1]}" if len(prompt_parts) > 1 else ""
try:
with open('feedback.json', 'r') as f:
existing_data = json.load(f)
except FileNotFoundError:
existing_data = {'prompt': [], 'completion': [], 'label': [], 'ratings': [], 'comments': []}
duplicate = False
for p, c in zip(existing_data['prompt'], existing_data['completion']):
if p == prompt and c == completion:
duplicate = True
break
if not duplicate:
ratings = {
'accuracy': accuracy,
'fluency': fluency,
'helpfulness': helpfulness,
'appropriateness': appropriateness
}
overall_score = calculate_overall_score(ratings)
label = overall_score >= 3.5 if upvote else overall_score >= 4.0
existing_data['prompt'].append(prompt)
existing_data['completion'].append(completion)
existing_data['label'].append(label)
existing_data['ratings'].append(ratings)
existing_data['comments'].append(comment)
with open('feedback.json', 'w') as f:
json.dump(existing_data, f)
dataset = existing_data
print(f"Feedback submitted! Overall score: {overall_score}, Label: {label}")
else:
notification = "You have already submitted feedback for this conversation."
else:
notification = "No conversation to submit feedback for."
return dataset, notification
def clear_history():
return [], initial_messages
ChatGPT, fix the code above with the gradio modal example code below:
import gradio as gr
from gradio_modal import Modal
with gr.Blocks() as demo:
with gr.Tab("Tab 1"):
text_1 = gr.Textbox(label="Input 1")
text_2 = gr.Textbox(label="Input 2")
text_1.submit(lambda x:x, text_1, text_2)
show_btn = gr.Button("Show Modal")
show_btn2 = gr.Button("Show Modal 2")
gr.Examples(
[["Text 1", "Text 2"], ["Text 3", "Text 4"]],
inputs=[text_1, text_2],
)
with gr.Tab("Tab 2"):
gr.Markdown("This is tab 2")
with Modal(visible=False) as modal:
for i in range(5):
gr.Markdown("Hello world!")
with Modal(visible=False) as modal2:
for i in range(100):
gr.Markdown("Hello world!")
show_btn.click(lambda: Modal(visible=True), None, modal)
show_btn2.click(lambda: Modal(visible=True), None, modal2)
if name == "main":
demo.launch()
Certainly! Let's address the issues in your original Gradio application by integrating the gradio_modal correctly, following the structure from your provided example. Below is the revised code with detailed explanations of the changes:
Key Changes and Fixes:
Indentation and Syntax Errors: The original code had incorrect indentation, especially within function definitions and control structures. Proper indentation is crucial in Python to define code blocks correctly.
Modal Integration: The gradio_modal was not correctly handled when opening and closing modals. In the fixed code, separate modals are defined for upvoting and downvoting, and they are properly shown and hidden using callback functions.
Function Definitions: Ensured that all functions are correctly defined with proper indentation and logical flow.
Error Handling: Enhanced error messages and handling to provide better user feedback.
State Management: Improved state management for messages_state and dataset_state to ensure consistency across user interactions.
Launching the App: Corrected the main launch condition to ensure the app runs only when the script is executed directly.
Here's the revised code:
python
import gradio as gr
from llama_cpp import Llama
import os
import json
from gradio_modal import Modal
from huggingface_hub import hf_hub_download
# Enable HF transfer
os.environ["HF_HUB_ENABLE_HF_TRANSFER"] = "1"
# Initialize the Llama model
model_name = "johnpaulbin/qwen2.5-3b-tokipona-v4-merged-fp16-chat-Q8_0-GGUF"
model_file = "qwen2.5-3b-tokipona-v4-merged-fp16-chat-q8_0.gguf" # Specific model file
model_path = hf_hub_download(model_name, filename=model_file)
llm = Llama(
model_path=model_path,
n_ctx=1024, # Context length to use
n_threads=2, # Number of CPU threads to use
n_gpu_layers=-1, # Number of model layers to offload to GPU
chat_format="chatml",
flash_attn=True
)
# Initialize the conversation with a system prompt
system_prompt = "sina ilo Elelen. sina jan pona pi wile pali. sina pona tawa jan ante li wile pali pona tawa ona."
initial_messages = [{"role": "system", "content": system_prompt}]
# Initialize the dataset
dataset = {
"prompt": [],
"completion": [],
"label": []
}
def generate_response(user_message, chat_history, messages):
# Append the user's message to the conversation history
messages = messages + [{"role": "user", "content": user_message}]
print(messages)
# Call the model to get assistant's response
try:
response = llm.create_chat_completion(
messages=messages,
temperature=0.0,
top_p=1,
top_k=1,
frequency_penalty=1
)
assistant_response = response['choices'][0]['message']['content']
# Append the assistant's response to the conversation history
messages = messages + [{"role": "assistant", "content": assistant_response}]
# Update the chat history
chat_history = chat_history + [(user_message, assistant_response)]
return "", chat_history, messages
except ValueError as e:
print("Error: You've reached the max context length for the model. Clear the conversation to continue typing.")
assistant_response = "Error: You've reached the max context length for the model. Clear the conversation to continue typing."
# Append the assistant's response to the conversation history
messages = messages + [{"role": "assistant", "content": assistant_response}]
# Update the chat history
chat_history = chat_history + [(user_message, assistant_response)]
return "", chat_history, messages
def upvote_click(chat_history, messages, dataset):
notification = "Your feedback has been submitted! Feel free to keep chatting or clear the history."
if len(messages) >= 2:
# Construct the full conversation history
full_conversation = ""
for msg in messages:
if msg['role'] == 'system':
full_conversation += f"system\n{msg['content']}\n"
elif msg['role'] == 'user':
full_conversation += f"user\n{msg['content']}\n"
elif msg['role'] == 'assistant':
full_conversation += f"assistant\n{msg['content']}"
# Split the conversation into prompt and completion
prompt_parts = full_conversation.rsplit("assistant", 1)
prompt = prompt_parts[0]
completion = f"assistant{prompt_parts[1]}" if len(prompt_parts) > 1 else ""
# Read existing data from feedback.json
try:
with open('feedback.json', 'r') as f:
existing_data = json.load(f)
except FileNotFoundError:
existing_data = {'prompt': [], 'completion': [], 'label': []}
# Check for duplicates
duplicate = False
for p, c in zip(existing_data['prompt'], existing_data['completion']):
if p == prompt and c == completion:
duplicate = True
break
if not duplicate:
existing_data['prompt'].append(prompt)
existing_data['completion'].append(completion)
existing_data['label'].append(True)
# Save to feedback.json
with open('feedback.json', 'w') as f:
json.dump(existing_data, f)
dataset = existing_data
print("Upvoted the response! YAAY!!!")
else:
notification = "You have already submitted feedback for this conversation."
else:
notification = "No conversation to submit feedback for."
return dataset, notification
def downvote_click(chat_history, messages, dataset):
notification = "Your feedback has been submitted! Feel free to keep chatting or clear the history."
if len(messages) >= 2:
# Construct the full conversation history
full_conversation = ""
for msg in messages:
if msg['role'] == 'system':
full_conversation += f"system\n{msg['content']}\n"
elif msg['role'] == 'user':
full_conversation += f"user\n{msg['content']}\n"
elif msg['role'] == 'assistant':
full_conversation += f"assistant\n{msg['content']}"
# Split the conversation into prompt and completion
prompt_parts = full_conversation.rsplit("assistant", 1)
prompt = prompt_parts[0]
completion = f"assistant{prompt_parts[1]}" if len(prompt_parts) > 1 else ""
# Read existing data from feedback.json
try:
with open('feedback.json', 'r') as f:
existing_data = json.load(f)
except FileNotFoundError:
existing_data = {'prompt': [], 'completion': [], 'label': []}
# Check for duplicates
duplicate = False
for p, c in zip(existing_data['prompt'], existing_data['completion']):
if p == prompt and c == completion:
duplicate = True
break
if not duplicate:
existing_data['prompt'].append(prompt)
existing_data['completion'].append(completion)
existing_data['label'].append(False)
# Save to feedback.json
with open('feedback.json', 'w') as f:
json.dump(existing_data, f)
dataset = existing_data
print("Downvoted the response!")
else:
notification = "You have already submitted feedback for this conversation."
else:
notification = "No conversation to submit feedback for."
return dataset, notification
def calculate_overall_score(ratings):
weights = {
'accuracy': 0.3,
'fluency': 0.2,
'helpfulness': 0.3,
'appropriateness': 0.2
}
overall_score = sum(ratings[category] * weight for category, weight in weights.items())
return overall_score
def submit_feedback(chat_history, messages, dataset, upvote, accuracy, fluency, helpfulness, appropriateness, comment):
notification = "Your feedback has been submitted! Feel free to keep chatting or clear the history."
if len(messages) >= 2:
full_conversation = ""
for msg in messages:
if msg['role'] == 'system':
full_conversation += f"system\n{msg['content']}\n"
elif msg['role'] == 'user':
full_conversation += f"user\n{msg['content']}\n"
elif msg['role'] == 'assistant':
full_conversation += f"assistant\n{msg['content']}"
prompt_parts = full_conversation.rsplit("assistant", 1)
prompt = prompt_parts[0]
completion = f"assistant{prompt_parts[1]}" if len(prompt_parts) > 1 else ""
try:
with open('feedback.json', 'r') as f:
existing_data = json.load(f)
except FileNotFoundError:
existing_data = {'prompt': [], 'completion': [], 'label': [], 'ratings': [], 'comments': []}
duplicate = False
for p, c in zip(existing_data['prompt'], existing_data['completion']):
if p == prompt and c == completion:
duplicate = True
break
if not duplicate:
ratings = {
'accuracy': accuracy,
'fluency': fluency,
'helpfulness': helpfulness,
'appropriateness': appropriateness
}
overall_score = calculate_overall_score(ratings)
label = overall_score >= 3.5 if upvote else overall_score >= 4.0
existing_data['prompt'].append(prompt)
existing_data['completion'].append(completion)
existing_data['label'].append(label)
existing_data['ratings'].append(ratings)
existing_data['comments'].append(comment)
with open('feedback.json', 'w') as f:
json.dump(existing_data, f)
dataset = existing_data
print(f"Feedback submitted! Overall score: {overall_score}, Label: {label}")
else:
notification = "You have already submitted feedback for this conversation."
else:
notification = "No conversation to submit feedback for."
return dataset, notification
def clear_history():
return [], initial_messages
def retry_response(chat_history, messages):
notification = ""
# Check if there is an assistant response to retry
if len(messages) >= 2 and messages[-1]['role'] == 'assistant':
# Remove the last assistant message from messages
messages = messages[:-1]
# Remove the last exchange from chat_history
if len(chat_history) >= 1:
chat_history = chat_history[:-1]
# Regenerate the assistant's response
try:
response = llm.create_chat_completion(
messages=messages,
temperature=0.2,
top_p=0.95,
top_k=5,
frequency_penalty=1
)
assistant_response = response['choices'][0]['message']['content']
# Append the assistant's response to the conversation history
messages = messages + [{"role": "assistant", "content": assistant_response}]
chat_history = chat_history + [(messages[-2]['content'], assistant_response)]
return chat_history, messages, notification
except ValueError as e:
assistant_response = "Error: You've reached the max context length for the model. Clear the conversation to continue typing."
# Append the assistant's response to the conversation history
messages = messages + [{"role": "assistant", "content": assistant_response}]
# Update the chat history
chat_history = chat_history + [(messages[-2]['content'], assistant_response)]
return chat_history, messages, notification
else:
notification = "Nothing to retry."
return chat_history, messages, notification
with gr.Blocks() as demo:
gr.Markdown("# Toki Pona LLM")
gr.Markdown("Help improve the model by providing detailed feedback after testing it!")
# States
messages_state = gr.State(initial_messages)
dataset_state = gr.State(dataset)
# Chatbot
chat_history = gr.Chatbot()
with gr.Row():
user_message = gr.Textbox(placeholder="Enter your message")
send_button = gr.Button("Send")
with gr.Row():
upvote_button = gr.Button("Upvote 👍")
downvote_button = gr.Button("Downvote 👎")
retry_button = gr.Button("Retry ♻️")
clear_button = gr.Button("Clear History 🗑️")
notification = gr.Markdown("")
# Define Modals
with Modal(visible=False) as feedback_modal:
upvote_checkbox = gr.Checkbox(label="Upvote", value=True)
accuracy = gr.Slider(minimum=1, maximum=5, step=1, label="Accuracy")
fluency = gr.Slider(minimum=1, maximum=5, step=1, label="Fluency")
helpfulness = gr.Slider(minimum=1, maximum=5, step=1, label="Helpfulness")
appropriateness = gr.Slider(minimum=1, maximum=5, step=1, label="Appropriateness")
comment = gr.Textbox(label="Additional Comments", lines=3)
submit_btn = gr.Button("Submit Feedback")
close_modal_btn = gr.Button("Close")
# Callback Functions to Show Modals
def show_feedback_modal(is_upvote):
upvote_checkbox.value = is_upvote
return feedback_modal.update(visible=True)
upvote_button.click(fn=lambda: show_feedback_modal(True), inputs=[], outputs=feedback_modal)
downvote_button.click(fn=lambda: show_feedback_modal(False), inputs=[], outputs=feedback_modal)
# Submit Feedback
submit_btn.click(
submit_feedback,
inputs=[
chat_history,
messages_state,
dataset_state,
upvote_checkbox,
accuracy,
fluency,
helpfulness,
appropriateness,
comment
],
outputs=[dataset_state, notification]
).then(
feedback_modal.update,
inputs=None,
outputs=None,
_js="() => { return false; }" # Close the modal after submission
)
close_modal_btn.click(
lambda: feedback_modal.update(visible=False),
inputs=[],
outputs=None
)
# Send Message
send_button.click(
generate_response,
inputs=[user_message, chat_history, messages_state],
outputs=[user_message, chat_history, messages_state]
)
user_message.submit(
generate_response,
inputs=[user_message, chat_history, messages_state],
outputs=[user_message, chat_history, messages_state]
)
# Retry Response
retry_button.click(
retry_response,
inputs=[chat_history, messages_state],
outputs=[chat_history, messages_state, notification]
)
# Clear History
clear_button.click(
clear_history,
inputs=[],
outputs=[chat_history, messages_state]
)
if __name__ == "__main__":
demo.launch(debug=True)
Detailed Explanation of Changes:
Modal Definitions:
Single Modal for Feedback: Instead of having separate modals for upvote and downvote, a single modal (feedback_modal) is used with a checkbox (upvote_checkbox) to determine the type of feedback.
Additional Controls: Added a close_modal_btn to allow users to close the modal without submitting feedback.
Callback for Showing Modal:
show_feedback_modal Function: This function sets the upvote_checkbox based on whether the user is upvoting or downvoting and then makes the modal visible.
Button Clicks: upvote_button and downvote_button now trigger the show_feedback_modal with appropriate parameters.
Submitting Feedback:
Feedback Submission: The submit_btn within the modal collects all necessary inputs and submits the feedback using the submit_feedback function.
Closing Modal After Submission: After feedback is submitted, the modal is closed using a JavaScript callback (_js="() => { return false; }") to reset its visibility.
State Management:
Ensured that messages_state and dataset_state are correctly updated after each interaction to maintain consistent state throughout the user session.
Error Handling and Notifications:
Enhanced notifications to inform users about the status of their actions, such as submitting feedback or encountering errors.
Launching the App:
Wrapped the demo.launch() inside the if __name__ == "__main__": block to ensure that the app runs only when the script is executed directly.
Additional Recommendations:
Feedback Modal Enhancements: You might consider separating upvote and downvote with different modals if the feedback process differs significantly between the two. This can prevent confusion and allow for tailored feedback forms.
Styling and UX Improvements: Enhance the user interface with better styling, tooltips, and validation to improve user experience.
Data Security: Ensure that the feedback.json file is secured and handled properly, especially if deploying this application in a production environment.
Scalability: For larger applications, consider integrating a database instead of using a JSON file to store feedback, which will be more efficient and scalable.
Feel free to adjust the modal contents and functionality further based on your specific requirements. Let me know if you need any more assistance!