Spaces:
Sleeping
Sleeping
Upload 3 files
Browse files- app.py +100 -0
- combined_analyzer.pkl +3 -0
- requirements.txt +4 -0
app.py
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
"""app.ipynb
|
| 3 |
+
|
| 4 |
+
Automatically generated by Colab.
|
| 5 |
+
|
| 6 |
+
Original file is located at
|
| 7 |
+
https://colab.research.google.com/drive/1EyjV73NmmvRi7U_r_GxUjz4X-5-6yMgE
|
| 8 |
+
"""
|
| 9 |
+
|
| 10 |
+
# load_and_run_pkl_model.py
|
| 11 |
+
# This script ONLY loads a pre-saved PKL file and runs a Gradio interface for it.
|
| 12 |
+
|
| 13 |
+
import gradio as gr
|
| 14 |
+
import pickle
|
| 15 |
+
import os
|
| 16 |
+
|
| 17 |
+
# --- Dependencies required ONLY for the class definition used by pickle ---
|
| 18 |
+
# We are NOT calling pipeline() here to load models from Hugging Face.
|
| 19 |
+
# Pickle just needs to know the *structure* of the saved object.
|
| 20 |
+
from transformers import pipeline
|
| 21 |
+
import torch
|
| 22 |
+
|
| 23 |
+
# --- Define the Class Structure ---
|
| 24 |
+
# IMPORTANT: This MUST exactly match the class definition used when
|
| 25 |
+
# the .pkl file was SAVED. Pickle needs this to reconstruct the object.
|
| 26 |
+
|
| 27 |
+
# --- Configuration: Path to your saved PKL file ---
|
| 28 |
+
PICKLE_FILENAME = "combined_analyzer.pkl"
|
| 29 |
+
MODEL_DIR = "saved_model" # Directory where the PKL file is located
|
| 30 |
+
PICKLE_FILEPATH = os.path.join(MODEL_DIR, PICKLE_FILENAME)
|
| 31 |
+
|
| 32 |
+
# --- Global Variable to hold the loaded analyzer ---
|
| 33 |
+
LOADED_ANALYZER = None
|
| 34 |
+
LOAD_ERROR_MESSAGE = None
|
| 35 |
+
|
| 36 |
+
# --- Load the Saved Model from PKL File ---
|
| 37 |
+
print("-" * 40)
|
| 38 |
+
print(f"Attempting to load analyzer object from: {PICKLE_FILEPATH}")
|
| 39 |
+
try:
|
| 40 |
+
# Security Note: Only unpickle files you trust.
|
| 41 |
+
if not os.path.exists(PICKLE_FILEPATH):
|
| 42 |
+
raise FileNotFoundError(f"Cannot find the model file at '{PICKLE_FILEPATH}'. Make sure it exists.")
|
| 43 |
+
|
| 44 |
+
with open(PICKLE_FILEPATH, 'rb') as f:
|
| 45 |
+
# The actual loading happens here
|
| 46 |
+
LOADED_ANALYZER = pickle.load(f)
|
| 47 |
+
|
| 48 |
+
# Validate the loaded object (basic check)
|
| 49 |
+
if not isinstance(LOADED_ANALYZER, CombinedAnalyzer) or not callable(getattr(LOADED_ANALYZER, 'analyze', None)):
|
| 50 |
+
LOADED_ANALYZER = None # Invalidate if it's not the right type or lacks the method
|
| 51 |
+
raise TypeError("The loaded object from the PKL file is not a valid 'CombinedAnalyzer' instance or is corrupted.")
|
| 52 |
+
else:
|
| 53 |
+
print(">>> Successfully loaded analyzer object from PKL file.")
|
| 54 |
+
|
| 55 |
+
except Exception as e:
|
| 56 |
+
LOAD_ERROR_MESSAGE = f"ERROR: Failed to load model from '{PICKLE_FILEPATH}'.\nDetails: {e}"
|
| 57 |
+
print(LOAD_ERROR_MESSAGE)
|
| 58 |
+
LOADED_ANALYZER = None # Ensure it's None if any error occurred
|
| 59 |
+
|
| 60 |
+
print("-" * 40)
|
| 61 |
+
|
| 62 |
+
# --- Gradio Function (Uses the globally loaded object) ---
|
| 63 |
+
def perform_analysis(input_text):
|
| 64 |
+
"""This function is called by the Gradio interface."""
|
| 65 |
+
if LOADED_ANALYZER is None:
|
| 66 |
+
# If loading failed earlier, return the error message
|
| 67 |
+
return LOAD_ERROR_MESSAGE or "Error: Analyzer model is not loaded."
|
| 68 |
+
|
| 69 |
+
if not input_text or not input_text.strip():
|
| 70 |
+
return "Please enter some text to analyze."
|
| 71 |
+
|
| 72 |
+
# Use the 'analyze' method of the object loaded from the PKL file
|
| 73 |
+
try:
|
| 74 |
+
results = LOADED_ANALYZER.analyze(input_text)
|
| 75 |
+
return results
|
| 76 |
+
except Exception as e:
|
| 77 |
+
# Catch errors during the analysis itself
|
| 78 |
+
print(f"Error during analysis execution: {e}")
|
| 79 |
+
return f"An error occurred during analysis:\n{e}"
|
| 80 |
+
|
| 81 |
+
# --- Create the Gradio Interface ---
|
| 82 |
+
gradio_app_description = f"Enter text to analyze using the model loaded from `{PICKLE_FILEPATH}`."
|
| 83 |
+
if LOADED_ANALYZER is None:
|
| 84 |
+
gradio_app_description += "\n\n**WARNING: Model failed to load. Analysis will not function.**"
|
| 85 |
+
|
| 86 |
+
interface = gr.Interface(
|
| 87 |
+
fn=perform_analysis, # The function that runs the analysis
|
| 88 |
+
inputs=gr.Textbox(lines=8, placeholder="Enter text here...", label="Input Text"),
|
| 89 |
+
outputs=gr.Textbox(lines=8, label="Analysis Results", interactive=False),
|
| 90 |
+
title="Analyze Text with Saved Model",
|
| 91 |
+
description=gradio_app_description,
|
| 92 |
+
allow_flagging='never'
|
| 93 |
+
)
|
| 94 |
+
|
| 95 |
+
# --- Launch the Gradio App ---
|
| 96 |
+
if __name__ == "__main__":
|
| 97 |
+
print("Launching Gradio interface...")
|
| 98 |
+
if LOADED_ANALYZER is None:
|
| 99 |
+
print("!!! Warning: Launching interface, but model loading failed. Check errors above. !!!")
|
| 100 |
+
interface.launch()
|
combined_analyzer.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:8e4dd11e04e895a5d1847965f97a25ad79fde1624a1ffe8d5d45c25b07afaaaa
|
| 3 |
+
size 768588802
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
transformers
|
| 2 |
+
torch
|
| 3 |
+
gradio
|
| 4 |
+
sentencepiece
|