Spaces:
Build error
Build error
Update app.py
Browse files25 unmodified lines26272829303132333435363738394041424344454647481 unmodified line505152535455565 unmodified lines6263646566676869707172737425 unmodified linesinputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True)with torch.no_grad():outputs = model(**inputs)probs = torch.nn.functional.softmax(outputs.logits, dim=-1)label_idx = torch.argmax(probs).item()predicted_label = labels[label_idx]# Loggingwith open(log_path, mode='a', newline='') as file:writer = csv.writer(file)writer.writerow([datetime.now().isoformat(), text, predicted_label])return {label: float(prob) for label, prob in zip(labels, probs[0])}, text, predicted_labeldef save_correction(text, predicted_label, user_correction):if user_correction != predicted_label:with open(corrections_path, mode='a', newline='') as file:writer = csv.writer(file)writer.writerow([datetime.now().isoformat(), text, predicted_label, user_correction])return f"Thanks! Correction recorded: {user_correction}"# Gradio UIwith gr.Blocks() as demo:1 unmodified linegr.Markdown("Enter a course review and get the sentiment prediction. You can correct the result if needed.")input_text = gr.Textbox(lines=4, placeholder="Enter course review here...")output_label = gr.Label(num_top_classes=3)predict_btn = gr.Button("Classify")with gr.Row(visible=False) as correction_row:5 unmodified lineshidden_text = gr.Textbox(visible=False)hidden_pred = gr.Textbox(visible=False)def show_correction_ui(result, text, pred):return result, gr.update(visible=True), text, predpredict_btn.click(classify_review, inputs=input_text, outputs=[output_label, hidden_text, hidden_pred])\.then(show_correction_ui, inputs=[output_label, hidden_text, hidden_pred],outputs=[output_label, correction_row, hidden_text, hidden_pred])submit_btn.click(save_correction, inputs=[hidden_text, hidden_pred, correction_dropdown], outputs=correction_status)25 unmodified lines26272829303132333435363738394041424344454647481 unmodified line505152535455565 unmodified lines62636465666768697071727325 unmodified linesinputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True)with torch.no_grad():outputs = model(**inputs)probs = torch.nn.functional.softmax(outputs.logits, dim=-1)[0]prob_dict = {label: float(prob) for label, prob in zip(labels, probs)}predicted_label = labels[torch.argmax(probs).item()]# Loggingwith open(log_path, mode='a', newline='') as file:writer = csv.writer(file)writer.writerow([datetime.now().isoformat(), text, predicted_label])return prob_dict, text, predicted_labeldef save_correction(text, predicted_label, user_correction):if user_correction != predicted_label:with open(corrections_path, mode='a', newline='') as file:writer = csv.writer(file)writer.writerow([datetime.now().isoformat(), text, predicted_label, user_correction])return f"✅ Thanks! Correction recorded: {user_correction}"# Gradio UIwith gr.Blocks() as demo:1 unmodified linegr.Markdown("Enter a course review and get the sentiment prediction. You can correct the result if needed.")input_text = gr.Textbox(lines=4, placeholder="Enter course review here...")output_label = gr.Label(num_top_classes=3, label="Predicted Sentiment")predict_btn = gr.Button("Classify")with gr.Row(visible=False) as correction_row:5 unmodified lineshidden_text = gr.Textbox(visible=False)hidden_pred = gr.Textbox(visible=False)def show_correction_ui(_, text, pred):return gr.update(visible=True), text, predpredict_btn.click(classify_review, inputs=input_text, outputs=[output_label, hidden_text, hidden_pred]) \.then(show_correction_ui, inputs=[output_label, hidden_text, hidden_pred],outputs=[correction_row, hidden_text, hidden_pred])submit_btn.click(save_correction, inputs=[hidden_text, hidden_pred, correction_dropdown], outputs=correction_status)
app.py+11-12