Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,4 @@
|
|
| 1 |
-
|
| 2 |
-
from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
|
| 3 |
-
import gradio as gr
|
| 4 |
-
from threading import Thread
|
| 5 |
import os
|
| 6 |
import json
|
| 7 |
import uuid
|
|
@@ -9,143 +6,14 @@ from datasets import Dataset
|
|
| 9 |
from huggingface_hub import HfApi, login
|
| 10 |
import time
|
| 11 |
|
| 12 |
-
# Install required packages if not present
|
| 13 |
-
from gradio_modal import Modal
|
| 14 |
-
import huggingface_hub
|
| 15 |
-
import datasets
|
| 16 |
-
|
| 17 |
-
# Model setup
|
| 18 |
-
checkpoint = "WillHeld/soft-raccoon"
|
| 19 |
-
device = "cuda"
|
| 20 |
-
tokenizer = AutoTokenizer.from_pretrained(checkpoint)
|
| 21 |
-
model = AutoModelForCausalLM.from_pretrained(checkpoint).to(device)
|
| 22 |
-
|
| 23 |
-
# Constants for dataset
|
| 24 |
-
DATASET_REPO = "WillHeld/model-feedback" # Replace with your username
|
| 25 |
-
DATASET_PATH = "./feedback_data" # Local path to store feedback
|
| 26 |
-
DATASET_FILENAME = "feedback.jsonl" # Filename for feedback data
|
| 27 |
-
|
| 28 |
-
# Ensure feedback directory exists
|
| 29 |
-
os.makedirs(DATASET_PATH, exist_ok=True)
|
| 30 |
-
|
| 31 |
-
# Feedback storage functions
|
| 32 |
-
def save_feedback_locally(conversation, satisfaction, feedback_text):
|
| 33 |
-
"""Save feedback to a local JSONL file"""
|
| 34 |
-
# Create a unique ID for this feedback entry
|
| 35 |
-
feedback_id = str(uuid.uuid4())
|
| 36 |
-
|
| 37 |
-
# Create a timestamp
|
| 38 |
-
timestamp = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
|
| 39 |
-
|
| 40 |
-
# Prepare the feedback data
|
| 41 |
-
feedback_data = {
|
| 42 |
-
"id": feedback_id,
|
| 43 |
-
"timestamp": timestamp,
|
| 44 |
-
"conversation": conversation,
|
| 45 |
-
"satisfaction": satisfaction,
|
| 46 |
-
"feedback": feedback_text
|
| 47 |
-
}
|
| 48 |
-
|
| 49 |
-
# Save to local file
|
| 50 |
-
feedback_file = os.path.join(DATASET_PATH, DATASET_FILENAME)
|
| 51 |
-
with open(feedback_file, "a") as f:
|
| 52 |
-
f.write(json.dumps(feedback_data) + "\n")
|
| 53 |
-
|
| 54 |
-
return feedback_id
|
| 55 |
-
|
| 56 |
-
def push_feedback_to_hub(hf_token=None):
|
| 57 |
-
"""Push the local feedback data to HuggingFace as a dataset"""
|
| 58 |
-
# Check if we have a token
|
| 59 |
-
if hf_token is None:
|
| 60 |
-
# Try to get token from environment variable
|
| 61 |
-
hf_token = os.environ.get("HF_TOKEN")
|
| 62 |
-
if hf_token is None:
|
| 63 |
-
print("No HuggingFace token provided. Cannot push to Hub.")
|
| 64 |
-
return False
|
| 65 |
-
|
| 66 |
-
try:
|
| 67 |
-
# Login to HuggingFace
|
| 68 |
-
login(token=hf_token)
|
| 69 |
-
|
| 70 |
-
# Check if we have data to push
|
| 71 |
-
feedback_file = os.path.join(DATASET_PATH, DATASET_FILENAME)
|
| 72 |
-
if not os.path.exists(feedback_file):
|
| 73 |
-
print("No feedback data to push.")
|
| 74 |
-
return False
|
| 75 |
-
|
| 76 |
-
# Load data from the JSONL file
|
| 77 |
-
with open(feedback_file, "r") as f:
|
| 78 |
-
feedback_data = [json.loads(line) for line in f]
|
| 79 |
-
|
| 80 |
-
# Create a dataset from the feedback data
|
| 81 |
-
dataset = Dataset.from_list(feedback_data)
|
| 82 |
-
|
| 83 |
-
# Push to Hub
|
| 84 |
-
dataset.push_to_hub(
|
| 85 |
-
DATASET_REPO,
|
| 86 |
-
private=True # Set to False if you want the dataset to be public
|
| 87 |
-
)
|
| 88 |
-
|
| 89 |
-
print(f"Feedback data pushed to {DATASET_REPO} successfully.")
|
| 90 |
-
return True
|
| 91 |
-
|
| 92 |
-
except Exception as e:
|
| 93 |
-
print(f"Error pushing feedback data to Hub: {e}")
|
| 94 |
-
return False
|
| 95 |
-
|
| 96 |
-
@spaces.GPU(duration=120)
|
| 97 |
-
def predict(message, history, temperature, top_p):
|
| 98 |
-
history.append({"role": "user", "content": message})
|
| 99 |
-
input_text = tokenizer.apply_chat_template(history, tokenize=False, add_generation_prompt=True)
|
| 100 |
-
inputs = tokenizer.encode(input_text, return_tensors="pt").to(device)
|
| 101 |
-
|
| 102 |
-
# Create a streamer
|
| 103 |
-
streamer = TextIteratorStreamer(tokenizer, skip_prompt=True, skip_special_tokens=True)
|
| 104 |
-
|
| 105 |
-
# Set up generation parameters
|
| 106 |
-
generation_kwargs = {
|
| 107 |
-
"input_ids": inputs,
|
| 108 |
-
"max_new_tokens": 1024,
|
| 109 |
-
"temperature": float(temperature),
|
| 110 |
-
"top_p": float(top_p),
|
| 111 |
-
"do_sample": True,
|
| 112 |
-
"streamer": streamer,
|
| 113 |
-
"eos_token_id": 128009,
|
| 114 |
-
}
|
| 115 |
-
|
| 116 |
-
# Run generation in a separate thread
|
| 117 |
-
thread = Thread(target=model.generate, kwargs=generation_kwargs)
|
| 118 |
-
thread.start()
|
| 119 |
-
|
| 120 |
-
# Yield from the streamer as tokens are generated
|
| 121 |
-
partial_text = ""
|
| 122 |
-
for new_text in streamer:
|
| 123 |
-
partial_text += new_text
|
| 124 |
-
yield partial_text
|
| 125 |
-
|
| 126 |
-
# Function to handle the research feedback submission
|
| 127 |
-
def submit_research_feedback(conversation_history, satisfaction, feedback_text):
|
| 128 |
-
"""Save user feedback both locally and to HuggingFace Hub"""
|
| 129 |
-
# Save locally first
|
| 130 |
-
feedback_id = save_feedback_locally(conversation_history, satisfaction, feedback_text)
|
| 131 |
-
|
| 132 |
-
# Get token from environment variable
|
| 133 |
-
env_token = os.environ.get("HF_TOKEN")
|
| 134 |
-
|
| 135 |
-
# Use environment token
|
| 136 |
-
push_success = push_feedback_to_hub(env_token)
|
| 137 |
-
|
| 138 |
-
if push_success:
|
| 139 |
-
status_msg = "Thank you for your valuable feedback! Your insights have been saved to the dataset."
|
| 140 |
-
else:
|
| 141 |
-
status_msg = "Thank you for your feedback! It has been saved locally, but couldn't be pushed to the dataset. Please check server logs."
|
| 142 |
-
|
| 143 |
-
return status_msg
|
| 144 |
-
|
| 145 |
# Create the Gradio interface
|
| 146 |
with gr.Blocks() as demo:
|
| 147 |
with gr.Row():
|
| 148 |
with gr.Column(scale=3):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 149 |
chatbot = gr.ChatInterface(
|
| 150 |
predict,
|
| 151 |
additional_inputs=[
|
|
@@ -154,6 +22,15 @@ with gr.Blocks() as demo:
|
|
| 154 |
],
|
| 155 |
type="messages"
|
| 156 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 157 |
|
| 158 |
with gr.Column(scale=1):
|
| 159 |
report_button = gr.Button("Share Feedback", variant="primary")
|
|
@@ -188,10 +65,7 @@ with gr.Blocks() as demo:
|
|
| 188 |
|
| 189 |
# Connect the submit button to the submit_research_feedback function with the current chat history
|
| 190 |
submit_button.click(
|
| 191 |
-
lambda satisfaction, feedback_text: submit_research_feedback(
|
| 192 |
-
inputs=[satisfaction, feedback_text],
|
| 193 |
outputs=response_text
|
| 194 |
-
)
|
| 195 |
-
|
| 196 |
-
# Launch the demo
|
| 197 |
-
demo.launch()
|
|
|
|
| 1 |
+
# Add this in your imports section if not already present
|
|
|
|
|
|
|
|
|
|
| 2 |
import os
|
| 3 |
import json
|
| 4 |
import uuid
|
|
|
|
| 6 |
from huggingface_hub import HfApi, login
|
| 7 |
import time
|
| 8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
# Create the Gradio interface
|
| 10 |
with gr.Blocks() as demo:
|
| 11 |
with gr.Row():
|
| 12 |
with gr.Column(scale=3):
|
| 13 |
+
# Create a State component to store the conversation history
|
| 14 |
+
chat_history = gr.State([])
|
| 15 |
+
|
| 16 |
+
# Create the ChatInterface
|
| 17 |
chatbot = gr.ChatInterface(
|
| 18 |
predict,
|
| 19 |
additional_inputs=[
|
|
|
|
| 22 |
],
|
| 23 |
type="messages"
|
| 24 |
)
|
| 25 |
+
|
| 26 |
+
# Create a function to update the chat history state
|
| 27 |
+
def update_history(message, history):
|
| 28 |
+
chat_history.value = history
|
| 29 |
+
return message, history
|
| 30 |
+
|
| 31 |
+
# Intercept chatbot responses to update our history state
|
| 32 |
+
# This requires modifying your predict function to pass through the history
|
| 33 |
+
# And connecting it to the update_history function
|
| 34 |
|
| 35 |
with gr.Column(scale=1):
|
| 36 |
report_button = gr.Button("Share Feedback", variant="primary")
|
|
|
|
| 65 |
|
| 66 |
# Connect the submit button to the submit_research_feedback function with the current chat history
|
| 67 |
submit_button.click(
|
| 68 |
+
lambda satisfaction, feedback_text, history: submit_research_feedback(history, satisfaction, feedback_text),
|
| 69 |
+
inputs=[satisfaction, feedback_text, chat_history],
|
| 70 |
outputs=response_text
|
| 71 |
+
)
|
|
|
|
|
|
|
|
|