Update app.py
Browse files
    	
        app.py
    CHANGED
    
    | 
         @@ -1,4 +1,7 @@ 
     | 
|
| 1 | 
         
            -
             
     | 
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 2 | 
         
             
            import os
         
     | 
| 3 | 
         
             
            import json
         
     | 
| 4 | 
         
             
            import uuid
         
     | 
| 
         @@ -6,14 +9,156 @@ from datasets import Dataset 
     | 
|
| 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,15 +167,6 @@ with gr.Blocks() as demo: 
     | 
|
| 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,7 +201,10 @@ with gr.Blocks() as demo: 
     | 
|
| 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 
     | 
| 69 | 
         
            -
                    inputs=[satisfaction, feedback_text 
     | 
| 70 | 
         
             
                    outputs=response_text
         
     | 
| 71 | 
         
            -
                )
         
     | 
| 
         | 
|
| 
         | 
|
| 
         | 
| 
         | 
|
| 1 | 
         
            +
            import spaces
         
     | 
| 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 | 
         
             
            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 | 
         
            +
            # Create a State to store chat history
         
     | 
| 97 | 
         
            +
            chat_history_state = []
         
     | 
| 98 | 
         
            +
             
     | 
| 99 | 
         
            +
            @spaces.GPU(duration=120)
         
     | 
| 100 | 
         
            +
            def predict(message, history, temperature, top_p):
         
     | 
| 101 | 
         
            +
                global chat_history_state
         
     | 
| 102 | 
         
            +
                
         
     | 
| 103 | 
         
            +
                # Update our chat history state
         
     | 
| 104 | 
         
            +
                history.append({"role": "user", "content": message})
         
     | 
| 105 | 
         
            +
                chat_history_state = history.copy()
         
     | 
| 106 | 
         
            +
                
         
     | 
| 107 | 
         
            +
                input_text = tokenizer.apply_chat_template(history, tokenize=False, add_generation_prompt=True)
         
     | 
| 108 | 
         
            +
                inputs = tokenizer.encode(input_text, return_tensors="pt").to(device)
         
     | 
| 109 | 
         
            +
                
         
     | 
| 110 | 
         
            +
                # Create a streamer
         
     | 
| 111 | 
         
            +
                streamer = TextIteratorStreamer(tokenizer, skip_prompt=True, skip_special_tokens=True)
         
     | 
| 112 | 
         
            +
                
         
     | 
| 113 | 
         
            +
                # Set up generation parameters
         
     | 
| 114 | 
         
            +
                generation_kwargs = {
         
     | 
| 115 | 
         
            +
                    "input_ids": inputs,
         
     | 
| 116 | 
         
            +
                    "max_new_tokens": 1024,
         
     | 
| 117 | 
         
            +
                    "temperature": float(temperature),
         
     | 
| 118 | 
         
            +
                    "top_p": float(top_p),
         
     | 
| 119 | 
         
            +
                    "do_sample": True,
         
     | 
| 120 | 
         
            +
                    "streamer": streamer,
         
     | 
| 121 | 
         
            +
                    "eos_token_id": 128009,
         
     | 
| 122 | 
         
            +
                }
         
     | 
| 123 | 
         
            +
                
         
     | 
| 124 | 
         
            +
                # Run generation in a separate thread
         
     | 
| 125 | 
         
            +
                thread = Thread(target=model.generate, kwargs=generation_kwargs)
         
     | 
| 126 | 
         
            +
                thread.start()
         
     | 
| 127 | 
         
            +
                
         
     | 
| 128 | 
         
            +
                # Yield from the streamer as tokens are generated
         
     | 
| 129 | 
         
            +
                partial_text = ""
         
     | 
| 130 | 
         
            +
                for new_text in streamer:
         
     | 
| 131 | 
         
            +
                    partial_text += new_text
         
     | 
| 132 | 
         
            +
                    yield partial_text
         
     | 
| 133 | 
         
            +
                
         
     | 
| 134 | 
         
            +
                # After generation is complete, update chat history state with the assistant response
         
     | 
| 135 | 
         
            +
                chat_history_state.append({"role": "assistant", "content": partial_text})
         
     | 
| 136 | 
         
            +
             
     | 
| 137 | 
         
            +
            # Function to handle the research feedback submission
         
     | 
| 138 | 
         
            +
            def submit_research_feedback(satisfaction, feedback_text):
         
     | 
| 139 | 
         
            +
                """Save user feedback both locally and to HuggingFace Hub"""
         
     | 
| 140 | 
         
            +
                global chat_history_state
         
     | 
| 141 | 
         
            +
                
         
     | 
| 142 | 
         
            +
                # Save locally first
         
     | 
| 143 | 
         
            +
                feedback_id = save_feedback_locally(chat_history_state, satisfaction, feedback_text)
         
     | 
| 144 | 
         
            +
                
         
     | 
| 145 | 
         
            +
                # Get token from environment variable
         
     | 
| 146 | 
         
            +
                env_token = os.environ.get("HF_TOKEN")
         
     | 
| 147 | 
         
            +
                
         
     | 
| 148 | 
         
            +
                # Use environment token
         
     | 
| 149 | 
         
            +
                push_success = push_feedback_to_hub(env_token)
         
     | 
| 150 | 
         
            +
                
         
     | 
| 151 | 
         
            +
                if push_success:
         
     | 
| 152 | 
         
            +
                    status_msg = "Thank you for your valuable feedback! Your insights have been saved to the dataset."
         
     | 
| 153 | 
         
            +
                else:
         
     | 
| 154 | 
         
            +
                    status_msg = "Thank you for your feedback! It has been saved locally, but couldn't be pushed to the dataset. Please check server logs."
         
     | 
| 155 | 
         
            +
                
         
     | 
| 156 | 
         
            +
                return status_msg
         
     | 
| 157 | 
         
            +
             
     | 
| 158 | 
         
             
            # Create the Gradio interface
         
     | 
| 159 | 
         
             
            with gr.Blocks() as demo:
         
     | 
| 160 | 
         
             
                with gr.Row():
         
     | 
| 161 | 
         
             
                    with gr.Column(scale=3):
         
     | 
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 162 | 
         
             
                        chatbot = gr.ChatInterface(
         
     | 
| 163 | 
         
             
                            predict,
         
     | 
| 164 | 
         
             
                            additional_inputs=[
         
     | 
| 
         | 
|
| 167 | 
         
             
                            ],
         
     | 
| 168 | 
         
             
                            type="messages"
         
     | 
| 169 | 
         
             
                        )
         
     | 
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 170 | 
         | 
| 171 | 
         
             
                    with gr.Column(scale=1):
         
     | 
| 172 | 
         
             
                        report_button = gr.Button("Share Feedback", variant="primary")
         
     | 
| 
         | 
|
| 201 | 
         | 
| 202 | 
         
             
                # Connect the submit button to the submit_research_feedback function with the current chat history
         
     | 
| 203 | 
         
             
                submit_button.click(
         
     | 
| 204 | 
         
            +
                    lambda satisfaction, feedback_text: submit_research_feedback(satisfaction, feedback_text),
         
     | 
| 205 | 
         
            +
                    inputs=[satisfaction, feedback_text],
         
     | 
| 206 | 
         
             
                    outputs=response_text
         
     | 
| 207 | 
         
            +
                )
         
     | 
| 208 | 
         
            +
             
     | 
| 209 | 
         
            +
            # Launch the demo
         
     | 
| 210 | 
         
            +
            demo.launch()
         
     |