ai code snippets 2

satya - 1/24/2024, 10:43:53 PM

How do I load spacy stuff


pip install spacy
python -m spacy download en_core_web_sm

# Then

import spacy
# Load the installed model
nlp = spacy.load("en_core_web_sm")

# Now you can process text using the model
doc = nlp("This is a sentence.")

satya - 1/24/2024, 10:46:49 PM

Vscode frustration

  1. venv sucks as pylance is not recognizing new modules although program works
  2. Remved venv and pycache
  3. Created a new work space with no venv
  4. For some reason vscode insists that I have a venv and disables the entire work space from running a command line terminal
  5. Started all over and have to explicitly "create" a work space instead of a folder to get the "basic" python work.
  6. No venv at this point
  7. Until I figure out pylance this is quite useless

satya - 1/25/2024, 8:39:57 AM

Workspaces, venv, and folders

  1. Each folder is common to have its own .venv
  2. Each folder corresponds to each project
  3. Vscode should allow where to place the .venv
  4. Shared virtual environments are possible but within the workspace
  5. within the workspace means, any root folder in that work space
  6. It is a common practice to have the workspace file outside of the root folders of that workspace

satya - 1/25/2024, 8:42:11 AM

How to debug the Pylance mess


Basic settings
*******************
"python.analysis.typeCheckingMode": "basic" 

"python.analysis.diagnosticSeverityOverrides": {
    "reportMissingImports": "error",
    "reportMissingModuleSource": "error",
    // other overrides
}

python: language server
*************************
Use "default" [or pylance]

In settings .json
*************************
{
  "python.analysis.extraPaths": [
    "/path/to/extra/library1",
    "/path/to/extra/library2"
  ]
}

More on the paths
***************************
{
  "python.analysis.extraPaths": [
    "./external_libs/lib1",
    "/absolute/path/to/lib2"
  ]
}

The "." is a root of the work space
Likely the ./.venv/libs/lib1
root folder of the project

Project/Folder level settings
**************************
.vscode/settings.json

satya - 1/25/2024, 4:47:30 PM

Streamlit conversational apps

Streamlit conversational apps

satya - 1/25/2024, 4:48:01 PM

streamlit and HF

streamlit and HF

satya - 1/25/2024, 4:48:30 PM

Github Sample code: Biju

Github Sample code: Biju

satya - 1/25/2024, 4:48:53 PM

Github Faisal

Github Faisal

satya - 1/25/2024, 5:53:39 PM

Here is what happens with Streamlit

  1. Just a python lib in your command line apps
  2. pip install it, just another lib (of course like all other python stuff the dependencies galore)
  3. You are ready to go
  4. If you run streamlit on a command line it creates a webapp on your local box
  5. It uses the python file you specify as its home page
  6. That .py file has write statements in it like hello world
  7. that is your first webapp!

satya - 1/25/2024, 5:55:26 PM

How long it took me: 1 hour for a basic page

  1. Went to their web page
  2. Read getting started
  3. wrote what I want in English
  4. Gave it to chatgpt
  5. I copied the code
  6. A web form with its output back to the page is the requirements
  7. About an hour

satya - 1/25/2024, 5:57:03 PM

Here is English instructions


Filename: streamlit-ui-requirements.txt
A file to guide ChatGPT based development

**********************************
Requirement 1: for ChatGPT: Create a webpage with a from submission
**********************************
What I want:

1. Create a function
2. it should prompt the user on command line with a prompt
3. Gather the prompt from user
4. If the prompt is empty exit the program
5. if the prompt is "q" or "quit" exactly exit the program
6. call a function to process the prompt. Call this function "processPrompt"
7. Put all this in a while loop
8. Call the function "executePromptLoop"

Assume:
1. To write code modularly

satya - 1/25/2024, 5:59:44 PM

Here is the code for that web page


import streamlit as st

#
#**************************************************
# Key elements
# 1. A form with a submit
# 2. Process text input
# 3. Write back what is entered so far
# 4. Rememebers with each web refresh the state
#**************************************************
#
def writeIntro():
    # Streamlit page layout
    st.title("Demo page")

    st.write("hello there")
    st.write("hello there")
    st.write("hello there")


def initialize_state():
    writeIntro()
    
    """Initialize the state variable."""
    if 'totalResponseText' not in st.session_state:
        st.session_state.totalResponseText = []

def writeOutput():
    # Display the accumulated text
    st.write("Accumulated Text:")
    for line in st.session_state.totalResponseText:
        st.write(line)


def process_input(input_text):
    """Append the input text to the totalResponseText state variable."""
    st.session_state.totalResponseText.append(input_text)

def main():
    """Main function to render the Streamlit page."""
    initialize_state()

    # Create a form
    with st.form(key='input_form'):
        text_input = st.text_input("Enter your text")
        submit_button = st.form_submit_button("Submit")

    # Process the form submission
    if submit_button and text_input:
        process_input(text_input)

    writeOutput()

#Kick it off
main()

satya - 1/27/2024, 11:43:11 AM

nltk docs

nltk docs

satya - 1/27/2024, 11:45:20 AM

nltk corpus api

nltk corpus api

satya - 1/27/2024, 11:47:05 AM

where is nltk shakespeare corpus documented?

where is nltk shakespeare corpus documented?

Search for: where is nltk shakespeare corpus documented?

satya - 1/27/2024, 11:52:58 AM

Closest I can find is here

Closest I can find is here

satya - 1/27/2024, 11:54:28 AM

wiki documentation on github

wiki documentation on github

satya - 1/27/2024, 11:54:36 AM

NLTK Book

NLTK Book

Search for: NLTK Book

satya - 1/27/2024, 11:55:10 AM

https://www.nltk.org/book/

https://www.nltk.org/book/

satya - 1/27/2024, 12:56:14 PM

Github, Martin Gorner, SOnnets text

Github, Martin Gorner, SOnnets text

satya - 1/27/2024, 1:08:23 PM

where is to download the shakespeare sonnets.txt file?

where is to download the shakespeare sonnets.txt file?

Search for: where is to download the shakespeare sonnets.txt file?

satya - 1/27/2024, 1:08:52 PM

Here is on gutenberg site

Here is on gutenberg site