Spaces:
Running
Running
demo
commited on
Commit
·
103a375
1
Parent(s):
ffd8f44
initial version
Browse files- .env +14 -0
- .gitignore +1 -0
- DEVELOPMENT.md +94 -0
- Dockerfile +24 -0
- LICENSE +21 -0
- README.md +54 -2
- app.py +70 -0
- compose.yaml +5 -0
- fn_diff.py +13 -0
- model_llama.py +86 -0
- model_llamacpp.py +81 -0
- model_wben.py +53 -0
- requirements.txt +7 -0
.env
ADDED
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
GRADIO_SERVER_IP="0.0.0.0"
|
2 |
+
GRADIO_SERVER_PORT=7860
|
3 |
+
|
4 |
+
TASK_ASR="automatic-speech-recognition"
|
5 |
+
MODEL_WHISPER1="openai/whisper-base.en"
|
6 |
+
MODEL_WHISPER1_FILE="model.safetensors"
|
7 |
+
|
8 |
+
TASK_TXTGEN="text-generation"
|
9 |
+
|
10 |
+
MODEL_LLAMA="TheBloke/Llama-2-7b-Chat-GGUF"
|
11 |
+
MODEL_LLAMA_FILE="llama-2-7b-chat.Q5_K_M.gguf"
|
12 |
+
|
13 |
+
MODEL_MISTRAL="TheBloke/Mistral-7B-Instruct-v0.2-GGUF"
|
14 |
+
MODEL_MISTRAL_FILE="mistral-7b-instruct-v0.2.Q5_K_M.gguf"
|
.gitignore
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
__pychache__
|
DEVELOPMENT.md
ADDED
@@ -0,0 +1,94 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Installation Notes for Development
|
2 |
+
|
3 |
+
## Running Eloquent App in Docker
|
4 |
+
The easiest way to install and run the Eloquent app is using the docker container from HuggingFace. You will need to have docker installed on your system (see prerequisites below) and then run the app container:
|
5 |
+
|
6 |
+
```
|
7 |
+
docker run -it -p 7860:7860 --platform=linux/amd64 \
|
8 |
+
registry.hf.space/vgotcheva-eloquent:latest
|
9 |
+
```
|
10 |
+
|
11 |
+
When the app container is fully started in docker, the app can be accessed in the browser with [http://localhost:7860](http://localhost:7860).
|
12 |
+
|
13 |
+
Eloquent App is a gradio app written in python. It uses ```automatic-speech-recognition``` and ```text-generation``` models through HuggingFace transformers library.
|
14 |
+
|
15 |
+
The app has been tested on a laptop with specifications:
|
16 |
+
- CPU: 11th Gen Intel(R) Core(TM) i7-1165G7 @ 2.80GHz
|
17 |
+
- RAM: 16.0 GB
|
18 |
+
- GPU: none
|
19 |
+
- SSD: at least 25GB available space
|
20 |
+
|
21 |
+
Note that the AI models (about 6GB total) will be downloaded and cached locally during the docker container start up. Therefore, it may take 10-15min for the app to start depending on the download speed.
|
22 |
+
|
23 |
+
## Running Eloquent App in Python Environment
|
24 |
+
The app can be run in a python development environment. Please, check the prerequisites below.
|
25 |
+
|
26 |
+
Clone the repository and cd in the cloned folder:
|
27 |
+
```
|
28 |
+
git clone https://huggingface.co/spaces/vgotcheva/eloquent
|
29 |
+
cd eloquent
|
30 |
+
```
|
31 |
+
|
32 |
+
Install the app requirements:
|
33 |
+
```
|
34 |
+
pip install --no-cache-dir --upgrade -r /code/requirements.txt
|
35 |
+
```
|
36 |
+
|
37 |
+
Start the app:
|
38 |
+
```
|
39 |
+
python app.py
|
40 |
+
```
|
41 |
+
|
42 |
+
Use the app by opening in browser [http://localhost:7860](http://localhost:7860).
|
43 |
+
|
44 |
+
## Prerequisites for development under Windows 11
|
45 |
+
|
46 |
+
### Visual Studio Code
|
47 |
+
https://code.visualstudio.com/
|
48 |
+
|
49 |
+
### Git for Windows including Git Credential Manager
|
50 |
+
https://gitforwindows.org/
|
51 |
+
|
52 |
+
### WSL 2 (with Git for Windows)
|
53 |
+
https://learn.microsoft.com/en-us/windows/wsl/install
|
54 |
+
|
55 |
+
### Linux Distribution in WSL
|
56 |
+
```
|
57 |
+
Ubuntu 20.04.6 LTS
|
58 |
+
```
|
59 |
+
|
60 |
+
### git and gif-lft
|
61 |
+
```bash
|
62 |
+
apt-get install git git-lfs
|
63 |
+
```
|
64 |
+
|
65 |
+
### Anaconda in Ubuntu
|
66 |
+
```bash
|
67 |
+
$ conda -V
|
68 |
+
conda 23.11.0
|
69 |
+
```
|
70 |
+
|
71 |
+
### conda virtual environment and pip
|
72 |
+
- https://conda.io/projects/conda/en/latest/user-guide/tasks/manage-environments.html#activating-an-environment
|
73 |
+
- https://anaconda.org/anaconda/pip
|
74 |
+
|
75 |
+
### Set up Git Credentials Manager in Ubuntu git
|
76 |
+
https://github.com/git-ecosystem/git-credential-manager/blob/release/docs/wsl.md
|
77 |
+
|
78 |
+
```
|
79 |
+
git config --global credential.helper '/mnt/c/Program Files/Git/mingw64/bin/git-credential-manager.exe'
|
80 |
+
```
|
81 |
+
or add it directly to
|
82 |
+
```
|
83 |
+
$ cat ~/.gitconfig
|
84 |
+
[user]
|
85 |
+
name = demo
|
86 |
+
email = demo@local
|
87 |
+
[credential]
|
88 |
+
helper = /mnt/c/Program\\ Files/Git/mingw64/bin/git-credential-manager.exe
|
89 |
+
```
|
90 |
+
|
91 |
+
https://docs.gitlab.com/ee/ci/ci_cd_for_external_repos/
|
92 |
+
|
93 |
+
### Docker Desktop WSL 2 backend on Windows
|
94 |
+
https://docs.docker.com/desktop/wsl/
|
Dockerfile
ADDED
@@ -0,0 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
FROM registry.hf.space/vgotcheva-base-dev:latest
|
2 |
+
#FROM python:3.9
|
3 |
+
#ARG GRADIO_SERVER_PORT=7000
|
4 |
+
#ENV GRADIO_SERVER_PORT=${GRADIO_SERVER_PORT}
|
5 |
+
|
6 |
+
RUN apt-get update && apt-get install -y python3 python3-pip
|
7 |
+
|
8 |
+
RUN useradd -m -u 1000 user
|
9 |
+
USER user
|
10 |
+
ENV HOME=/home/user \
|
11 |
+
PATH=/home/user/.local/bin:$PATH
|
12 |
+
|
13 |
+
WORKDIR $HOME/app
|
14 |
+
COPY --chown=user . $HOME/app
|
15 |
+
|
16 |
+
#WORKDIR /code
|
17 |
+
#COPY ./requirements.txt /code/requirements.txt
|
18 |
+
RUN pip install --no-cache-dir --upgrade pip
|
19 |
+
RUN pip install --no-cache-dir --upgrade -r requirements.txt
|
20 |
+
|
21 |
+
# Needed for caching the llama model due to its large size
|
22 |
+
RUN python -c "import model_llama as ml; ml.cached_folder"
|
23 |
+
|
24 |
+
CMD ["python", "app.py"]
|
LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
MIT License
|
2 |
+
|
3 |
+
Copyright (c) 2024 Violeta Gotcheva
|
4 |
+
|
5 |
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6 |
+
of this software and associated documentation files (the "Software"), to deal
|
7 |
+
in the Software without restriction, including without limitation the rights
|
8 |
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9 |
+
copies of the Software, and to permit persons to whom the Software is
|
10 |
+
furnished to do so, subject to the following conditions:
|
11 |
+
|
12 |
+
The above copyright notice and this permission notice shall be included in all
|
13 |
+
copies or substantial portions of the Software.
|
14 |
+
|
15 |
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16 |
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17 |
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18 |
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19 |
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20 |
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21 |
+
SOFTWARE.
|
README.md
CHANGED
@@ -1,11 +1,63 @@
|
|
1 |
---
|
2 |
-
title: Eloquent
|
3 |
-
emoji:
|
4 |
colorFrom: blue
|
5 |
colorTo: indigo
|
6 |
sdk: docker
|
7 |
pinned: false
|
|
|
8 |
license: cc0-1.0
|
|
|
9 |
---
|
10 |
|
11 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
---
|
2 |
+
title: Eloquent
|
3 |
+
emoji: 👀
|
4 |
colorFrom: blue
|
5 |
colorTo: indigo
|
6 |
sdk: docker
|
7 |
pinned: false
|
8 |
+
startup_duration_timeout: 1h
|
9 |
license: cc0-1.0
|
10 |
+
app_port: 7860
|
11 |
---
|
12 |
|
13 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
14 |
+
|
15 |
+
# Eloquent App: A private coach to speaking clearly
|
16 |
+
|
17 |
+
## Introduction
|
18 |
+
Eloquent App is a gradio app written in python. The app has been tested offline on a laptop with specifications:
|
19 |
+
- CPU: 11th Gen Intel(R) Core(TM) i7-1165G7 @ 2.80GHz
|
20 |
+
- RAM: 16.0 GB
|
21 |
+
- GPU: none
|
22 |
+
- SSD: at least 25GB available space
|
23 |
+
|
24 |
+
The app uses ```automatic-speech-recognition``` and ```text-generation``` models through HuggingFace supported libraries.
|
25 |
+
The AI models are about 6GB total. They will be downloaded and cached locally when the app is started for the first time. Therefore, network connection is required and it may take 10-15min for the app to start depending on the download speed.
|
26 |
+
|
27 |
+
## How to run the app on local computer
|
28 |
+
Clone from the GitLab repository:
|
29 |
+
```bash
|
30 |
+
git clone https://gitlab.com/vgotcheva/eloquent-gitlab.git
|
31 |
+
```
|
32 |
+
|
33 |
+
Then go in the cloned folder ```cd eloquent``` and install python dependencies:
|
34 |
+
```
|
35 |
+
pip install --no-cache-dir --upgrade -r requirements.txt
|
36 |
+
```
|
37 |
+
|
38 |
+
Run the app.py with python:
|
39 |
+
```
|
40 |
+
python app.py
|
41 |
+
```
|
42 |
+
|
43 |
+
## How to run the app in docker on local computer
|
44 |
+
You will need docker and docker compose installed and started on your local computer. [Get Docker](https://docs.docker.com/get-docker/)
|
45 |
+
|
46 |
+
Go in the cloned folder ```cd eloquent``` and run docker compose:
|
47 |
+
```
|
48 |
+
docker compose up -d
|
49 |
+
```
|
50 |
+
The above command will build the image when it is executed for the first time and then start a docker container with image.
|
51 |
+
|
52 |
+
Verify that the image is created and container running
|
53 |
+
```
|
54 |
+
docker image ls
|
55 |
+
docker ps
|
56 |
+
```
|
57 |
+
|
58 |
+
## How to use the app:
|
59 |
+
Use the app by opening it in your browser:
|
60 |
+
http://0.0.0.0:7860
|
61 |
+
|
62 |
+
## Development Environment
|
63 |
+
For notes on setting a development environment read [DEVELOPMENT.md](DEVELOPMENT.md)
|
app.py
ADDED
@@ -0,0 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
from dotenv import load_dotenv
|
3 |
+
import numpy as np
|
4 |
+
import gradio as gr
|
5 |
+
|
6 |
+
# eloquent imports
|
7 |
+
import model_wben as asr
|
8 |
+
import model_llama as llm
|
9 |
+
#import model_llamacpp as llm
|
10 |
+
import fn_diff as dff
|
11 |
+
|
12 |
+
load_dotenv()
|
13 |
+
|
14 |
+
## Front-End
|
15 |
+
with gr.Blocks() as demo:
|
16 |
+
# record audio
|
17 |
+
gr.Markdown(
|
18 |
+
"""
|
19 |
+
# Eloquent is ready.
|
20 |
+
Press the Record.
|
21 |
+
Start Speaking.
|
22 |
+
Press Stop.
|
23 |
+
Click on Transcribe.
|
24 |
+
Transcription and improved text will appear below.
|
25 |
+
"""
|
26 |
+
)
|
27 |
+
|
28 |
+
myspeech = gr.Audio(sources=["microphone"])
|
29 |
+
|
30 |
+
with gr.Row():
|
31 |
+
with gr.Column():
|
32 |
+
# transcribe
|
33 |
+
b1 = gr.Button("Click to Transcribe Audio")
|
34 |
+
mytranscription = gr.Textbox(
|
35 |
+
label="speech transcription",
|
36 |
+
autoscroll=True,
|
37 |
+
max_lines=5
|
38 |
+
)
|
39 |
+
|
40 |
+
with gr.Column():
|
41 |
+
b2 = gr.Button("Click to Improve Transcription")
|
42 |
+
myspeech_improved = gr.Textbox(
|
43 |
+
label="improved speech"
|
44 |
+
)
|
45 |
+
|
46 |
+
myspeech_diff = gr.HighlightedText(
|
47 |
+
label="transcript and improved speech differences",
|
48 |
+
combine_adjacent=True,
|
49 |
+
show_legend=True,
|
50 |
+
color_map={"+": "green", "-": "red"})
|
51 |
+
|
52 |
+
b1.click(fn=asr.transcribe, inputs=myspeech, outputs=mytranscription)
|
53 |
+
|
54 |
+
b2.click(fn=llm.improve_grammar,
|
55 |
+
inputs=[mytranscription],
|
56 |
+
outputs=[myspeech_improved]
|
57 |
+
)
|
58 |
+
|
59 |
+
myspeech_improved.change(fn=dff.diff_texts,
|
60 |
+
inputs=[mytranscription,myspeech_improved],
|
61 |
+
outputs=myspeech_diff
|
62 |
+
)
|
63 |
+
|
64 |
+
if __name__ == "__main__":
|
65 |
+
demo.queue()
|
66 |
+
demo.launch(
|
67 |
+
share=False,
|
68 |
+
server_name=os.getenv('GRADIO_SERVER_IP'),
|
69 |
+
server_port=int(os.getenv('GRADIO_SERVER_PORT'))
|
70 |
+
)
|
compose.yaml
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
services:
|
2 |
+
app:
|
3 |
+
build: .
|
4 |
+
ports:
|
5 |
+
- "${GRADIO_SERVER_IP}:${GRADIO_SERVER_PORT}:${GRADIO_SERVER_PORT}"
|
fn_diff.py
ADDED
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from difflib import Differ
|
2 |
+
|
3 |
+
def diff_texts(text1, text2):
|
4 |
+
d = Differ()
|
5 |
+
|
6 |
+
d3= []
|
7 |
+
for token in d.compare(text1, text2):
|
8 |
+
if token[0] != '-':
|
9 |
+
if token[0] != " ":
|
10 |
+
d3.append((token[2:], token[0]))
|
11 |
+
else:
|
12 |
+
d3.append((token[2:], None))
|
13 |
+
return d3
|
model_llama.py
ADDED
@@ -0,0 +1,86 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
from dotenv import load_dotenv
|
3 |
+
import numpy as np
|
4 |
+
from huggingface_hub import snapshot_download
|
5 |
+
#from difflib import Differ
|
6 |
+
import gradio as gr
|
7 |
+
#from transformers import pipeline
|
8 |
+
#from llama_cpp import Llama
|
9 |
+
from ctransformers import AutoModelForCausalLM
|
10 |
+
|
11 |
+
load_dotenv()
|
12 |
+
|
13 |
+
task_asr=os.getenv('TASK_TXTGEN')
|
14 |
+
model_id=os.getenv('MODEL_MISTRAL')
|
15 |
+
model_file=os.getenv('MODEL_MISTRAL_FILE')
|
16 |
+
|
17 |
+
cached_folder=snapshot_download(
|
18 |
+
repo_id=model_id,
|
19 |
+
allow_patterns=model_file,
|
20 |
+
local_dir=None
|
21 |
+
)
|
22 |
+
|
23 |
+
def get_cached_folder():
|
24 |
+
return cached_folder
|
25 |
+
|
26 |
+
llm = AutoModelForCausalLM.from_pretrained(
|
27 |
+
model_id,
|
28 |
+
model_file=model_file,
|
29 |
+
model_type="llama"
|
30 |
+
# gpu_layers=50
|
31 |
+
)
|
32 |
+
|
33 |
+
B_INST, E_INST = "<s>[INST]", "[/INST]"
|
34 |
+
B_SYS, E_SYS = "<<SYS>>\n", "\n<</SYS>>\n\n"
|
35 |
+
DEFAULT_SYSTEM_PROMPT = """\
|
36 |
+
You are a helpful, respectful and honest assistant. Always answer as helpfully as possible, while being safe. Your answers should not include any harmful, unethical, racist, sexist, toxic, dangerous, or illegal content. Please ensure that your responses are socially unbiased and positive in nature.
|
37 |
+
|
38 |
+
If a question does not make any sense, or is not factually coherent, explain why instead of answering something not correct. If you don't know the answer to a question, please don't share false information.
|
39 |
+
|
40 |
+
Correct the grammar in the user text.
|
41 |
+
|
42 |
+
"""
|
43 |
+
|
44 |
+
SYSTEM_PROMPT = B_SYS + DEFAULT_SYSTEM_PROMPT + E_SYS
|
45 |
+
|
46 |
+
def get_prompt(instruction):
|
47 |
+
prompt_template = B_INST + SYSTEM_PROMPT + instruction + E_INST
|
48 |
+
return prompt_template
|
49 |
+
|
50 |
+
def improve_grammar(text_input):
|
51 |
+
#https://huggingface.co/mzbac/mistral-grammar
|
52 |
+
#https://llama-cpp-python.readthedocs.io/en/latest/
|
53 |
+
|
54 |
+
input_prompt = get_prompt(text_input)
|
55 |
+
|
56 |
+
text_output = llm(
|
57 |
+
input_prompt,
|
58 |
+
temperature=0.0,
|
59 |
+
# top_p=0.1,
|
60 |
+
# top_k=40,
|
61 |
+
# repeat_penalty=1.1,
|
62 |
+
# max_tokens=2048,
|
63 |
+
# n_ctx=2048,
|
64 |
+
# echo=False
|
65 |
+
)
|
66 |
+
return text_output
|
67 |
+
|
68 |
+
if __name__ == "__main__":
|
69 |
+
with gr.Blocks() as demo:
|
70 |
+
mytextinput = gr.Textbox("This is a test box.")
|
71 |
+
b1 = gr.Button("Click to improve text")
|
72 |
+
mytextimproved = gr.Textbox(
|
73 |
+
label="Improved text"
|
74 |
+
)
|
75 |
+
b1.click(
|
76 |
+
fn=improve_grammar,
|
77 |
+
inputs=mytextinput,
|
78 |
+
outputs=mytextimproved
|
79 |
+
)
|
80 |
+
|
81 |
+
demo.queue()
|
82 |
+
demo.launch(
|
83 |
+
share=False,
|
84 |
+
server_name=os.getenv('GRADIO_SERVER_IP'),
|
85 |
+
server_port=int(os.getenv('GRADIO_SERVER_PORT'))
|
86 |
+
)
|
model_llamacpp.py
ADDED
@@ -0,0 +1,81 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
from dotenv import load_dotenv
|
3 |
+
import numpy as np
|
4 |
+
from huggingface_hub import snapshot_download
|
5 |
+
from llama_cpp import Llama
|
6 |
+
import gradio as gr
|
7 |
+
|
8 |
+
load_dotenv()
|
9 |
+
|
10 |
+
task_asr=os.getenv('TASK_TXTGEN')
|
11 |
+
model_id=os.getenv('MODEL_MISTRAL')
|
12 |
+
model_file=os.getenv('MODEL_MISTRAL_FILE')
|
13 |
+
|
14 |
+
cached_file=snapshot_download(
|
15 |
+
repo_id=model_id,
|
16 |
+
allow_patterns=model_file,
|
17 |
+
local_dir=None
|
18 |
+
)
|
19 |
+
|
20 |
+
def get_cached_file():
|
21 |
+
return cached_file
|
22 |
+
|
23 |
+
llm = Llama(
|
24 |
+
model_path=cached_file + "/" + model_file,
|
25 |
+
verbose=False
|
26 |
+
)
|
27 |
+
|
28 |
+
B_INST, E_INST = "<s>[INST]", "[/INST]"
|
29 |
+
B_SYS, E_SYS = "<<SYS>>\n", "\n<</SYS>>\n\n"
|
30 |
+
DEFAULT_SYSTEM_PROMPT = """\
|
31 |
+
You are a helpful, respectful and honest assistant. Always answer as helpfully as possible, while being safe. Your answers should not include any harmful, unethical, racist, sexist, toxic, dangerous, or illegal content. Please ensure that your responses are socially unbiased and positive in nature.
|
32 |
+
|
33 |
+
If a question does not make any sense, or is not factually coherent, explain why instead of answering something not correct. If you don't know the answer to a question, please don't share false information.
|
34 |
+
|
35 |
+
Correct the grammar in the user text.
|
36 |
+
|
37 |
+
"""
|
38 |
+
|
39 |
+
SYSTEM_PROMPT = B_SYS + DEFAULT_SYSTEM_PROMPT + E_SYS
|
40 |
+
|
41 |
+
def get_prompt(instruction):
|
42 |
+
prompt_template = B_INST + SYSTEM_PROMPT + instruction + E_INST
|
43 |
+
return prompt_template
|
44 |
+
|
45 |
+
def improve_grammar(text_input):
|
46 |
+
#https://huggingface.co/mzbac/mistral-grammar
|
47 |
+
#https://llama-cpp-python.readthedocs.io/en/latest/
|
48 |
+
|
49 |
+
input_prompt = get_prompt(text_input)
|
50 |
+
|
51 |
+
text_output = llm(
|
52 |
+
input_prompt,
|
53 |
+
temperature=0.0,
|
54 |
+
top_p=0.1,
|
55 |
+
top_k=40,
|
56 |
+
repeat_penalty=1.1,
|
57 |
+
max_tokens=2048,
|
58 |
+
# n_ctx=2048,
|
59 |
+
echo=False
|
60 |
+
)
|
61 |
+
return text_output['choices'][0]['text']
|
62 |
+
|
63 |
+
if __name__ == "__main__":
|
64 |
+
with gr.Blocks() as demo:
|
65 |
+
mytextinput = gr.Textbox("This is a test box.")
|
66 |
+
b1 = gr.Button("Click to improve text")
|
67 |
+
mytextimproved = gr.Textbox(
|
68 |
+
label="Improved text"
|
69 |
+
)
|
70 |
+
b1.click(
|
71 |
+
fn=improve_grammar,
|
72 |
+
inputs=mytextinput,
|
73 |
+
outputs=mytextimproved
|
74 |
+
)
|
75 |
+
|
76 |
+
demo.queue()
|
77 |
+
demo.launch(
|
78 |
+
share=False,
|
79 |
+
server_name=os.getenv('GRADIO_SERVER_IP'),
|
80 |
+
server_port=int(os.getenv('GRADIO_SERVER_PORT'))
|
81 |
+
)
|
model_wben.py
ADDED
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
from dotenv import load_dotenv
|
3 |
+
from huggingface_hub import snapshot_download
|
4 |
+
import numpy as np
|
5 |
+
import gradio as gr
|
6 |
+
from transformers import pipeline
|
7 |
+
|
8 |
+
load_dotenv()
|
9 |
+
|
10 |
+
task_asr=os.getenv('TASK_ASR')
|
11 |
+
model_id=os.getenv('MODEL_WHISPER1')
|
12 |
+
model_file=os.getenv('MODEL_WHISPER1_FILE')
|
13 |
+
|
14 |
+
def get_model_file():
|
15 |
+
cached_file=snapshot_download(
|
16 |
+
repo_id=model_id,
|
17 |
+
allow_patterns=model_file,
|
18 |
+
local_dir=None
|
19 |
+
)
|
20 |
+
return cached_file
|
21 |
+
|
22 |
+
|
23 |
+
asr = pipeline(task_asr, model=model_id)
|
24 |
+
|
25 |
+
def transcribe(audio):
|
26 |
+
sr, y = audio
|
27 |
+
y = y.astype(np.float32)
|
28 |
+
y /= np.max(np.abs(y))
|
29 |
+
|
30 |
+
return asr({"sampling_rate": sr, "raw": y})["text"]
|
31 |
+
|
32 |
+
if __name__ == "__main__":
|
33 |
+
with gr.Blocks() as demo:
|
34 |
+
myspeech = gr.Audio(sources=["microphone"])
|
35 |
+
b1 = gr.Button("Click to Transcribe")
|
36 |
+
mytranscription = gr.Textbox(
|
37 |
+
label="speech transcription",
|
38 |
+
autoscroll=True,
|
39 |
+
max_lines=5
|
40 |
+
)
|
41 |
+
|
42 |
+
b1.click(
|
43 |
+
fn=transcribe,
|
44 |
+
inputs=myspeech,
|
45 |
+
outputs=mytranscription
|
46 |
+
)
|
47 |
+
|
48 |
+
demo.queue()
|
49 |
+
demo.launch(
|
50 |
+
share=False,
|
51 |
+
server_name=os.getenv('GRADIO_SERVER_IP'),
|
52 |
+
server_port=int(os.getenv('GRADIO_SERVER_PORT'))
|
53 |
+
)
|
requirements.txt
ADDED
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
python-dotenv==0.21.0
|
2 |
+
gradio==4.16.0
|
3 |
+
transformers==4.37.2
|
4 |
+
ctransformers==0.2.27
|
5 |
+
torch==2.2.0
|
6 |
+
torchaudio==2.2.0
|
7 |
+
llama_cpp_python==0.2.44
|