Spaces:
				
			
			
	
			
			
		Runtime error
		
	
	
	
			
			
	
	
	
	
		
		
		Runtime error
		
	
		jaifar530
		
	commited on
		
		
					Fix indintation
Browse files
    	
        app.py
    CHANGED
    
    | @@ -5,7 +5,6 @@ from keras.models import load_model | |
| 5 | 
             
            from keras.preprocessing.text import Tokenizer
         | 
| 6 | 
             
            from keras.preprocessing.sequence import pad_sequences
         | 
| 7 | 
             
            from sklearn.preprocessing import LabelEncoder
         | 
| 8 | 
            -
            #from nltk.tokenize import word_tokenize  # Assuming you've imported this for word_tokenize
         | 
| 9 | 
             
            import pickle
         | 
| 10 | 
             
            import numpy as np
         | 
| 11 | 
             
            import streamlit as st
         | 
| @@ -31,69 +30,67 @@ if not os.path.exists('my_authorship_model'): | |
| 31 | 
             
                    st.write(f"Downloaded model size: {len(r.content)} bytes")
         | 
| 32 |  | 
| 33 | 
             
                    # Save the downloaded content
         | 
| 34 | 
            -
            with open(zip_file_path, "wb") as f:
         | 
| 35 | 
             
                        f.write(r.content)
         | 
| 36 |  | 
| 37 | 
            -
            Debugging: Verify that the zip file exists
         | 
| 38 | 
             
                    if os.path.exists(zip_file_path):
         | 
| 39 | 
             
                        st.write("Zip file exists")
         | 
| 40 |  | 
| 41 | 
            -
            Debugging: List contents of the zip file using unzip
         | 
| 42 | 
             
                        subprocess.run(['unzip', '-l', zip_file_path])
         | 
| 43 |  | 
| 44 | 
             
                        # Extract the model using unzip
         | 
| 45 | 
            -
                        unzip_result = subprocess.run(['unzip', '-o', zip_file_path, '-d','my_authorship_model'])
         | 
| 46 |  | 
| 47 | 
             
                        # Debugging: Check unzip exit code (0 means success)
         | 
| 48 | 
             
                        if unzip_result.returncode == 0:
         | 
| 49 | 
            -
            st.write | 
| 50 | 
             
                            # Debugging: List the directory contents after extraction
         | 
| 51 | 
             
                            st.write("Listing directory contents:")
         | 
| 52 | 
             
                            st.write(os.listdir('.'))
         | 
| 53 | 
            -
                            
         | 
| 54 | 
             
                        else:
         | 
| 55 | 
             
                            st.write("Model folder was not extracted successfully using unzip")
         | 
| 56 | 
             
                            exit(1)
         | 
| 57 | 
             
                    else:
         | 
| 58 | 
             
                        st.write("Zip file does not exist")
         | 
| 59 | 
             
                        exit(1)
         | 
| 60 | 
            -
            except Exception as e:
         | 
| 61 | 
             
                    st.write(f"Failed to download or extract the model: {e}")
         | 
| 62 | 
             
                    exit(1)
         | 
| 63 | 
             
            else:
         | 
| 64 | 
             
                st.write("Model folder exists")
         | 
| 65 |  | 
| 66 | 
            -
            Debugging: Print current working directory after extraction
         | 
| 67 | 
             
            st.write(f"Current Working Directory After Extraction: {os.getcwd()}")
         | 
| 68 |  | 
| 69 | 
            -
             | 
| 70 | 
            -
            Debugging: Check if model folder contains required files
         | 
| 71 | 
             
            try:
         | 
| 72 | 
             
                model_files = os.listdir('my_authorship_model')
         | 
| 73 | 
             
                st.write(f"Files in model folder: {model_files}")
         | 
| 74 | 
             
            except Exception as e:
         | 
| 75 | 
            -
            st.write(f | 
| 76 |  | 
| 77 | 
            -
            Download the required files
         | 
| 78 | 
             
            file_urls = {
         | 
| 79 | 
             
                'tokenizer.pkl': 'https://jaifar.net/ADS/tokenizer.pkl',
         | 
| 80 | 
             
                'label_encoder.pkl': 'https://jaifar.net/ADS/label_encoder.pkl'
         | 
| 81 | 
             
            }
         | 
| 82 |  | 
| 83 | 
            -
            for filename  | 
| 84 | 
             
                try:
         | 
| 85 | 
             
                    r = requests.get(url, headers=headers)
         | 
| 86 | 
             
                    r.raise_for_status()
         | 
| 87 | 
            -
            with open(filename, 'wb') as f:
         | 
| 88 | 
             
                        f.write(r.content)
         | 
| 89 | 
            -
            except Exception as e:
         | 
| 90 | 
             
                    st.write(f"Failed to download {filename}: {e}")
         | 
| 91 | 
             
                    exit(1)
         | 
| 92 |  | 
| 93 | 
            -
            Load the saved model
         | 
| 94 | 
             
            loaded_model = load_model("my_authorship_model")
         | 
| 95 |  | 
| 96 | 
            -
            Load the saved tokenizer and label encoder
         | 
| 97 | 
             
            with open('tokenizer.pkl', 'rb') as handle:
         | 
| 98 | 
             
                tokenizer = pickle.load(handle)
         | 
| 99 |  | 
| @@ -107,21 +104,18 @@ def predict_author(new_text, model, tokenizer, label_encoder): | |
| 107 | 
             
                sequence = tokenizer.texts_to_sequences([new_text])
         | 
| 108 | 
             
                padded_sequence = pad_sequences(sequence, maxlen=max_length, padding='post', truncating='post')
         | 
| 109 | 
             
                prediction = model.predict(padded_sequence)
         | 
| 110 | 
            -
             | 
| 111 | 
             
                predicted_label = label_encoder.inverse_transform([prediction.argmax()])[0]
         | 
| 112 | 
             
                probabilities = prediction[0]
         | 
| 113 | 
             
                author_probabilities = {}
         | 
| 114 | 
             
                for idx, prob in enumerate(probabilities):
         | 
| 115 | 
             
                    author = label_encoder.inverse_transform([idx])[0]
         | 
| 116 | 
             
                    author_probabilities[author] = prob
         | 
| 117 | 
            -
             | 
| 118 | 
             
                return predicted_label, author_probabilities
         | 
| 119 |  | 
| 120 | 
             
            st.markdown("CNN : version: 1.2")
         | 
| 121 | 
             
            new_text = st.text_area("Input your text here")
         | 
| 122 | 
            -
            #words_counts = word_tokenize(new_text)  # Changed input_paragraph to new_text
         | 
| 123 | 
            -
            #final_words = len(words_counts)
         | 
| 124 | 
            -
            #st.write('Words counts: ', final_words)
         | 
| 125 |  | 
| 126 | 
             
            # Creates a button named 'Press me'
         | 
| 127 | 
             
            press_me_button = st.button("Which Model Used?")
         | 
| @@ -129,7 +123,7 @@ press_me_button = st.button("Which Model Used?") | |
| 129 | 
             
            if press_me_button:
         | 
| 130 | 
             
                predicted_author, author_probabilities = predict_author(new_text, loaded_model, tokenizer, label_encoder)
         | 
| 131 | 
             
                sorted_probabilities = sorted(author_probabilities.items(), key=lambda x: x[1], reverse=True)
         | 
| 132 | 
            -
             | 
| 133 | 
             
                st.write(f"The text is most likely written by: {predicted_author}")
         | 
| 134 | 
             
                st.write("Probabilities for each author are (sorted):")
         | 
| 135 | 
             
                for author, prob in sorted_probabilities:
         | 
|  | |
| 5 | 
             
            from keras.preprocessing.text import Tokenizer
         | 
| 6 | 
             
            from keras.preprocessing.sequence import pad_sequences
         | 
| 7 | 
             
            from sklearn.preprocessing import LabelEncoder
         | 
|  | |
| 8 | 
             
            import pickle
         | 
| 9 | 
             
            import numpy as np
         | 
| 10 | 
             
            import streamlit as st
         | 
|  | |
| 30 | 
             
                    st.write(f"Downloaded model size: {len(r.content)} bytes")
         | 
| 31 |  | 
| 32 | 
             
                    # Save the downloaded content
         | 
| 33 | 
            +
                    with open(zip_file_path, "wb") as f:
         | 
| 34 | 
             
                        f.write(r.content)
         | 
| 35 |  | 
| 36 | 
            +
                    # Debugging: Verify that the zip file exists
         | 
| 37 | 
             
                    if os.path.exists(zip_file_path):
         | 
| 38 | 
             
                        st.write("Zip file exists")
         | 
| 39 |  | 
| 40 | 
            +
                        # Debugging: List contents of the zip file using unzip
         | 
| 41 | 
             
                        subprocess.run(['unzip', '-l', zip_file_path])
         | 
| 42 |  | 
| 43 | 
             
                        # Extract the model using unzip
         | 
| 44 | 
            +
                        unzip_result = subprocess.run(['unzip', '-o', zip_file_path, '-d', 'my_authorship_model'])
         | 
| 45 |  | 
| 46 | 
             
                        # Debugging: Check unzip exit code (0 means success)
         | 
| 47 | 
             
                        if unzip_result.returncode == 0:
         | 
| 48 | 
            +
                            st.write("Model folder successfully extracted using unzip")
         | 
| 49 | 
             
                            # Debugging: List the directory contents after extraction
         | 
| 50 | 
             
                            st.write("Listing directory contents:")
         | 
| 51 | 
             
                            st.write(os.listdir('.'))
         | 
|  | |
| 52 | 
             
                        else:
         | 
| 53 | 
             
                            st.write("Model folder was not extracted successfully using unzip")
         | 
| 54 | 
             
                            exit(1)
         | 
| 55 | 
             
                    else:
         | 
| 56 | 
             
                        st.write("Zip file does not exist")
         | 
| 57 | 
             
                        exit(1)
         | 
| 58 | 
            +
                except Exception as e:
         | 
| 59 | 
             
                    st.write(f"Failed to download or extract the model: {e}")
         | 
| 60 | 
             
                    exit(1)
         | 
| 61 | 
             
            else:
         | 
| 62 | 
             
                st.write("Model folder exists")
         | 
| 63 |  | 
| 64 | 
            +
            # Debugging: Print current working directory after extraction
         | 
| 65 | 
             
            st.write(f"Current Working Directory After Extraction: {os.getcwd()}")
         | 
| 66 |  | 
| 67 | 
            +
            # Debugging: Check if model folder contains required files
         | 
|  | |
| 68 | 
             
            try:
         | 
| 69 | 
             
                model_files = os.listdir('my_authorship_model')
         | 
| 70 | 
             
                st.write(f"Files in model folder: {model_files}")
         | 
| 71 | 
             
            except Exception as e:
         | 
| 72 | 
            +
                st.write(f"Could not list files in model folder: {e}")
         | 
| 73 |  | 
| 74 | 
            +
            # Download the required files
         | 
| 75 | 
             
            file_urls = {
         | 
| 76 | 
             
                'tokenizer.pkl': 'https://jaifar.net/ADS/tokenizer.pkl',
         | 
| 77 | 
             
                'label_encoder.pkl': 'https://jaifar.net/ADS/label_encoder.pkl'
         | 
| 78 | 
             
            }
         | 
| 79 |  | 
| 80 | 
            +
            for filename, url in file_urls.items():
         | 
| 81 | 
             
                try:
         | 
| 82 | 
             
                    r = requests.get(url, headers=headers)
         | 
| 83 | 
             
                    r.raise_for_status()
         | 
| 84 | 
            +
                    with open(filename, 'wb') as f:
         | 
| 85 | 
             
                        f.write(r.content)
         | 
| 86 | 
            +
                except Exception as e:
         | 
| 87 | 
             
                    st.write(f"Failed to download {filename}: {e}")
         | 
| 88 | 
             
                    exit(1)
         | 
| 89 |  | 
| 90 | 
            +
            # Load the saved model
         | 
| 91 | 
             
            loaded_model = load_model("my_authorship_model")
         | 
| 92 |  | 
| 93 | 
            +
            # Load the saved tokenizer and label encoder
         | 
| 94 | 
             
            with open('tokenizer.pkl', 'rb') as handle:
         | 
| 95 | 
             
                tokenizer = pickle.load(handle)
         | 
| 96 |  | 
|  | |
| 104 | 
             
                sequence = tokenizer.texts_to_sequences([new_text])
         | 
| 105 | 
             
                padded_sequence = pad_sequences(sequence, maxlen=max_length, padding='post', truncating='post')
         | 
| 106 | 
             
                prediction = model.predict(padded_sequence)
         | 
| 107 | 
            +
             | 
| 108 | 
             
                predicted_label = label_encoder.inverse_transform([prediction.argmax()])[0]
         | 
| 109 | 
             
                probabilities = prediction[0]
         | 
| 110 | 
             
                author_probabilities = {}
         | 
| 111 | 
             
                for idx, prob in enumerate(probabilities):
         | 
| 112 | 
             
                    author = label_encoder.inverse_transform([idx])[0]
         | 
| 113 | 
             
                    author_probabilities[author] = prob
         | 
| 114 | 
            +
             | 
| 115 | 
             
                return predicted_label, author_probabilities
         | 
| 116 |  | 
| 117 | 
             
            st.markdown("CNN : version: 1.2")
         | 
| 118 | 
             
            new_text = st.text_area("Input your text here")
         | 
|  | |
|  | |
|  | |
| 119 |  | 
| 120 | 
             
            # Creates a button named 'Press me'
         | 
| 121 | 
             
            press_me_button = st.button("Which Model Used?")
         | 
|  | |
| 123 | 
             
            if press_me_button:
         | 
| 124 | 
             
                predicted_author, author_probabilities = predict_author(new_text, loaded_model, tokenizer, label_encoder)
         | 
| 125 | 
             
                sorted_probabilities = sorted(author_probabilities.items(), key=lambda x: x[1], reverse=True)
         | 
| 126 | 
            +
             | 
| 127 | 
             
                st.write(f"The text is most likely written by: {predicted_author}")
         | 
| 128 | 
             
                st.write("Probabilities for each author are (sorted):")
         | 
| 129 | 
             
                for author, prob in sorted_probabilities:
         | 
