Tobias Geisler commited on
Commit
0a440b6
·
1 Parent(s): 8566571

template init: simple chatbot

Browse files
Files changed (5) hide show
  1. .gitignore +59 -0
  2. README_DEV.md +131 -0
  3. app.py +30 -0
  4. requirements.txt +111 -0
  5. utils.py +27 -0
.gitignore ADDED
@@ -0,0 +1,59 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Virtual environment
2
+ gradio-env/
3
+
4
+ # Python cache files
5
+ __pycache__/
6
+ *.py[cod]
7
+ *$py.class
8
+
9
+ # C extensions
10
+ *.so
11
+
12
+ # Distribution / packaging
13
+ .Python
14
+ build/
15
+ develop-eggs/
16
+ dist/
17
+ downloads/
18
+ eggs/
19
+ .eggs/
20
+ lib/
21
+ lib64/
22
+ parts/
23
+ sdist/
24
+ var/
25
+ wheels/
26
+ *.egg-info/
27
+ .installed.cfg
28
+ *.egg
29
+
30
+ # PyInstaller
31
+ *.manifest
32
+ *.spec
33
+
34
+ # Jupyter Notebook
35
+ .ipynb_checkpoints
36
+
37
+ # PyCharm
38
+ .idea/
39
+
40
+ # VS Code
41
+ .vscode/
42
+
43
+ # Environment variables
44
+ .env
45
+
46
+ # OS generated files
47
+ .DS_Store
48
+ .DS_Store?
49
+ ._*
50
+ .Spotlight-V100
51
+ .Trashes
52
+ ehthumbs.db
53
+ Thumbs.db
54
+
55
+ # Gradio specific
56
+ flagged/
57
+
58
+ # Logs
59
+ *.log
README_DEV.md ADDED
@@ -0,0 +1,131 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ To run Gradio apps locally using Windows Subsystem for Linux (WSL) and Visual Studio Code (VSCode), follow these steps:
2
+
3
+ ## **1. Set Up WSL**
4
+
5
+ ### **Enable WSL**
6
+ 1. **Windows Features Dialog:**
7
+ - Open the Windows search bar, type "features," and select *Turn Windows Features on or off*.
8
+ - Scroll down and check *Windows Subsystem for Linux*.
9
+ - Click *OK* and restart your computer.
10
+
11
+ 2. **PowerShell:**
12
+ - Open PowerShell as an Administrator and run:
13
+ ```powershell
14
+ Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux
15
+ ```
16
+ - Restart your computer when prompted.
17
+
18
+ ### **Install a Linux Distro**
19
+ 1. Open the Microsoft Store and search for a Linux distribution (e.g., Ubuntu).
20
+ 2. Install and launch the distribution.
21
+ 3. Follow the prompts to complete the installation and create a user ID and password.
22
+
23
+ ## **2. Set Up Visual Studio Code with WSL**
24
+
25
+ ### **Install VSCode and WSL Extension**
26
+ 1. Download and install [Visual Studio Code](https://code.visualstudio.com/).
27
+ 2. Install the *Remote - WSL* extension from the VSCode marketplace.
28
+
29
+ ### **Open a WSL Terminal in VSCode**
30
+ 1. Open your WSL terminal (e.g., Ubuntu).
31
+ 2. Navigate to your project directory.
32
+ 3. Type `code .` to open the current directory in VSCode.
33
+
34
+ ## **3. Install and Run Gradio in a Virtual Environment**
35
+
36
+ ### **Create a Virtual Environment**
37
+ 1. In your WSL terminal, navigate to your project directory:
38
+ ```bash
39
+ cd /path/to/your/project
40
+ ```
41
+ 2. Create a virtual environment:
42
+ ```bash
43
+ python3 -m venv gradio-env
44
+ ```
45
+ 3. Activate the virtual environment:
46
+ ```bash
47
+ source gradio-env/bin/activate
48
+ ```
49
+
50
+ ### **Install Gradio**
51
+ 1. With the virtual environment activated, install Gradio:
52
+ ```bash
53
+ pip install gradio
54
+ ```
55
+
56
+ ### **Run the App**
57
+
58
+ ```
59
+ 2. Run the Gradio app:
60
+ ```bash
61
+ python app.py
62
+ ```
63
+ 3. The app will open in a browser at `http://localhost:7860`.
64
+
65
+ ### **Hot Reload Mode (Optional)**
66
+ For development convenience, you can use Gradio's hot reload mode:
67
+ ```bash
68
+ gradio app.py
69
+ ```
70
+
71
+ This setup allows you to develop and run Gradio apps efficiently using WSL and VSCode, leveraging the power of a Linux environment on a Windows machine[1][3][4][5].
72
+
73
+ Citations:
74
+ [1] https://code.visualstudio.com/docs/remote/wsl-tutorial
75
+ [2] https://learn.microsoft.com/en-us/windows/ai/toolkit/toolkit-fine-tune
76
+ [3] https://www.gradio.app/guides/quickstart
77
+ [4] https://code.visualstudio.com/docs/remote/wsl
78
+ [5] https://www.gradio.app/guides/installing-gradio-in-a-virtual-environment
79
+
80
+ To automatically create and update a requirements.txt file for your Python application, you have several options. Here are the most effective methods:
81
+
82
+ # Updating the requirements.txt
83
+
84
+ ## **Using pipreqs (Recommended)**
85
+
86
+ Pipreqs is a third-party tool that analyzes your project's imports and generates a more accurate requirements.txt file[4][8].
87
+
88
+ 1. Install pipreqs:
89
+ ```bash
90
+ pip install pipreqs
91
+ ```
92
+ 2. Navigate to your project directory.
93
+ 3. Run pipreqs:
94
+ ```bash
95
+ pipreqs .
96
+ ```
97
+
98
+ Pipreqs will scan your project, identify the imports, and create a requirements.txt file with only the packages your project actually uses[4][8].
99
+
100
+ ## **TODO Automating with GitHub Actions**
101
+
102
+ For projects hosted on GitHub, you can automate the process using GitHub Actions:
103
+
104
+ 1. Create a workflow file (e.g., `.github/workflows/update-requirements.yml`).
105
+ 2. Use an action like `timmypidashev/auto-pipreqs-cpy@master` to automatically generate the requirements.txt file[5].
106
+
107
+ ## **Best Practices**
108
+
109
+ 1. **Use virtual environments**: This ensures your requirements.txt only includes project-specific dependencies[4].
110
+ 2. **Specify versions**: Use `==` to pin exact versions for reproducibility[4].
111
+ 3. **Update regularly**: Keep your requirements.txt up to date as you add or remove dependencies[4].
112
+ 4. **Review generated files**: Always review automatically generated requirements.txt files to ensure they're accurate and complete[6].
113
+
114
+ By following these methods and best practices, you can efficiently manage your Python project's dependencies and ensure consistency across different environments.
115
+
116
+ Citations:
117
+ [1] https://stackoverflow.com/questions/31684375/automatically-create-file-requirements-txt
118
+ [2] https://www.mend.io/free-developer-tools/a/community/software-dependencies/how-can-i-automatically-generate-and-update-a-requirements-txt-file-from-a-pipfile-using-pipenv/
119
+ [3] https://stackoverflow.com/questions/24764549/upgrade-python-packages-from-requirements-txt-using-pip-command
120
+ [4] https://www.squash.io/how-to-automatically-create-requirementstxt-in-python/
121
+ [5] https://github.com/marketplace/actions/automatic-requirements-txt-generation-for-python-projects
122
+ [6] https://pynwb.readthedocs.io/en/stable/update_requirements.html
123
+ [7] https://www.geeksforgeeks.org/how-to-create-requirements-txt-file-in-python/
124
+ [8] https://www.youtube.com/watch?v=0ZeoruSobc4
125
+ [9] https://www.paleblueapps.com/rockandnull/python-update-requirements-txt/
126
+ [10] https://www.scaler.com/topics/how-to-create-requirements-txt-python/
127
+ [11] https://learn.microsoft.com/en-us/visualstudio/python/managing-required-packages-with-requirements-txt?view=vs-2022
128
+ [12] https://www.jetbrains.com/help/pycharm/managing-dependencies.html
129
+ [13] https://note.nkmk.me/en/python-pip-install-requirements/
130
+ [14] https://www.freecodecamp.org/news/python-requirementstxt-explained/
131
+ [15] https://learnpython.com/blog/python-requirements-file/
app.py ADDED
@@ -0,0 +1,30 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from openai import OpenAI
3
+ from utils import get_secret
4
+
5
+ # Initialize the OpenAI client
6
+ client = OpenAI(api_key=get_secret('OPENAI_API_KEY'))
7
+
8
+ def generate_response(prompt):
9
+ try:
10
+ response = client.chat.completions.create(
11
+ model="gpt-3.5-turbo",
12
+ messages=[
13
+ {"role": "system", "content": "You are a helpful assistant."},
14
+ {"role": "user", "content": prompt}
15
+ ]
16
+ )
17
+ return response.choices[0].message.content
18
+ except Exception as e:
19
+ return str(e)
20
+
21
+ demo = gr.Interface(
22
+ fn=generate_response,
23
+ inputs="text",
24
+ outputs="text",
25
+ title="OpenAI GPT-3.5 Turbo Chatbot",
26
+ description="Enter a prompt to get a response from OpenAI's GPT-3.5 Turbo model."
27
+ )
28
+
29
+ if __name__ == "__main__":
30
+ demo.launch()
requirements.txt ADDED
@@ -0,0 +1,111 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ aiofiles==23.2.1
2
+ altair==5.3.0
3
+ annotated-types==0.7.0
4
+ anyio==4.4.0
5
+ asttokens==2.4.1
6
+ attrs==23.2.0
7
+ backcall==0.2.0
8
+ beautifulsoup4==4.12.3
9
+ bleach==6.1.0
10
+ build==1.2.1
11
+ certifi==2024.7.4
12
+ charset-normalizer==3.3.2
13
+ click==8.1.7
14
+ contourpy==1.2.1
15
+ cycler==0.12.1
16
+ decorator==5.1.1
17
+ defusedxml==0.7.1
18
+ distro==1.9.0
19
+ dnspython==2.6.1
20
+ docopt==0.6.2
21
+ email_validator==2.2.0
22
+ executing==2.0.1
23
+ fastapi==0.111.1
24
+ fastapi-cli==0.0.4
25
+ fastjsonschema==2.20.0
26
+ ffmpy==0.3.2
27
+ filelock==3.15.4
28
+ fonttools==4.53.1
29
+ fsspec==2024.6.1
30
+ gradio==4.38.1
31
+ gradio_client==1.1.0
32
+ h11==0.14.0
33
+ httpcore==1.0.5
34
+ httptools==0.6.1
35
+ httpx==0.27.0
36
+ huggingface-hub==0.23.5
37
+ idna==3.7
38
+ importlib_resources==6.4.0
39
+ ipython==8.12.3
40
+ jedi==0.19.1
41
+ Jinja2==3.1.4
42
+ jsonschema==4.23.0
43
+ jsonschema-specifications==2023.12.1
44
+ jupyter_client==8.6.2
45
+ jupyter_core==5.7.2
46
+ jupyterlab_pygments==0.3.0
47
+ kiwisolver==1.4.5
48
+ markdown-it-py==3.0.0
49
+ MarkupSafe==2.1.5
50
+ matplotlib==3.9.1
51
+ matplotlib-inline==0.1.7
52
+ mdurl==0.1.2
53
+ mistune==3.0.2
54
+ nbclient==0.10.0
55
+ nbconvert==7.16.4
56
+ nbformat==5.10.4
57
+ numpy==2.0.0
58
+ openai==1.35.14
59
+ orjson==3.10.6
60
+ packaging==24.1
61
+ pandas==2.2.2
62
+ pandocfilters==1.5.1
63
+ parso==0.8.4
64
+ pexpect==4.9.0
65
+ pickleshare==0.7.5
66
+ pillow==10.4.0
67
+ platformdirs==4.2.2
68
+ prompt_toolkit==3.0.47
69
+ ptyprocess==0.7.0
70
+ pure-eval==0.2.2
71
+ pydantic==2.8.2
72
+ pydantic_core==2.20.1
73
+ pydub==0.25.1
74
+ Pygments==2.18.0
75
+ pyparsing==3.1.2
76
+ pyproject_hooks==1.1.0
77
+ python-dateutil==2.9.0.post0
78
+ python-dotenv==1.0.1
79
+ python-multipart==0.0.9
80
+ pytz==2024.1
81
+ PyYAML==6.0.1
82
+ pyzmq==26.0.3
83
+ referencing==0.35.1
84
+ requests==2.32.3
85
+ rich==13.7.1
86
+ rpds-py==0.19.0
87
+ ruff==0.5.2
88
+ semantic-version==2.10.0
89
+ shellingham==1.5.4
90
+ six==1.16.0
91
+ sniffio==1.3.1
92
+ soupsieve==2.5
93
+ stack-data==0.6.3
94
+ starlette==0.37.2
95
+ tinycss2==1.3.0
96
+ tomlkit==0.12.0
97
+ toolz==0.12.1
98
+ tornado==6.4.1
99
+ tqdm==4.66.4
100
+ traitlets==5.14.3
101
+ typer==0.12.3
102
+ typing_extensions==4.12.2
103
+ tzdata==2024.1
104
+ urllib3==2.2.2
105
+ uvicorn==0.30.1
106
+ uvloop==0.19.0
107
+ watchfiles==0.22.0
108
+ wcwidth==0.2.13
109
+ webencodings==0.5.1
110
+ websockets==11.0.3
111
+ yarg==0.1.9
utils.py ADDED
@@ -0,0 +1,27 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ from dotenv import load_dotenv
3
+
4
+ def get_secret(key: str, default: str = None) -> str:
5
+ """
6
+ Retrieve a secret from environment variables or .env file.
7
+
8
+ Args:
9
+ key (str): The name of the secret/environment variable.
10
+ default (str, optional): A default value if the secret is not found.
11
+
12
+ Returns:
13
+ str: The value of the secret, or the default value if not found.
14
+ """
15
+ # Load .env file if it exists (for local development)
16
+ load_dotenv()
17
+
18
+ # Try to get the secret from environment variables
19
+ secret = os.getenv(key)
20
+
21
+ if secret is None:
22
+ if default is not None:
23
+ return default
24
+ else:
25
+ raise ValueError(f"Secret '{key}' not found in environment variables and no default provided.")
26
+
27
+ return secret