douglarek commited on
Commit
798b452
1 Parent(s): 105fd41

Initial commit

Browse files
Files changed (6) hide show
  1. .gitignore +162 -0
  2. .python-version +1 -0
  3. app.py +135 -0
  4. env.example +1 -0
  5. requirements-dev.txt +4 -0
  6. requirements.txt +3 -0
.gitignore ADDED
@@ -0,0 +1,162 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Byte-compiled / optimized / DLL files
2
+ __pycache__/
3
+ *.py[cod]
4
+ *$py.class
5
+
6
+ # C extensions
7
+ *.so
8
+
9
+ # Distribution / packaging
10
+ .Python
11
+ build/
12
+ develop-eggs/
13
+ dist/
14
+ downloads/
15
+ eggs/
16
+ .eggs/
17
+ lib/
18
+ lib64/
19
+ parts/
20
+ sdist/
21
+ var/
22
+ wheels/
23
+ share/python-wheels/
24
+ *.egg-info/
25
+ .installed.cfg
26
+ *.egg
27
+ MANIFEST
28
+
29
+ # PyInstaller
30
+ # Usually these files are written by a python script from a template
31
+ # before PyInstaller builds the exe, so as to inject date/other infos into it.
32
+ *.manifest
33
+ *.spec
34
+
35
+ # Installer logs
36
+ pip-log.txt
37
+ pip-delete-this-directory.txt
38
+
39
+ # Unit test / coverage reports
40
+ htmlcov/
41
+ .tox/
42
+ .nox/
43
+ .coverage
44
+ .coverage.*
45
+ .cache
46
+ nosetests.xml
47
+ coverage.xml
48
+ *.cover
49
+ *.py,cover
50
+ .hypothesis/
51
+ .pytest_cache/
52
+ cover/
53
+
54
+ # Translations
55
+ *.mo
56
+ *.pot
57
+
58
+ # Django stuff:
59
+ *.log
60
+ local_settings.py
61
+ db.sqlite3
62
+ db.sqlite3-journal
63
+
64
+ # Flask stuff:
65
+ instance/
66
+ .webassets-cache
67
+
68
+ # Scrapy stuff:
69
+ .scrapy
70
+
71
+ # Sphinx documentation
72
+ docs/_build/
73
+
74
+ # PyBuilder
75
+ .pybuilder/
76
+ target/
77
+
78
+ # Jupyter Notebook
79
+ .ipynb_checkpoints
80
+
81
+ # IPython
82
+ profile_default/
83
+ ipython_config.py
84
+
85
+ # pyenv
86
+ # For a library or package, you might want to ignore these files since the code is
87
+ # intended to run in multiple environments; otherwise, check them in:
88
+ # .python-version
89
+
90
+ # pipenv
91
+ # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
92
+ # However, in case of collaboration, if having platform-specific dependencies or dependencies
93
+ # having no cross-platform support, pipenv may install dependencies that don't work, or not
94
+ # install all needed dependencies.
95
+ #Pipfile.lock
96
+
97
+ # poetry
98
+ # Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
99
+ # This is especially recommended for binary packages to ensure reproducibility, and is more
100
+ # commonly ignored for libraries.
101
+ # https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
102
+ #poetry.lock
103
+
104
+ # pdm
105
+ # Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
106
+ #pdm.lock
107
+ # pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
108
+ # in version control.
109
+ # https://pdm.fming.dev/#use-with-ide
110
+ .pdm.toml
111
+
112
+ # PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
113
+ __pypackages__/
114
+
115
+ # Celery stuff
116
+ celerybeat-schedule
117
+ celerybeat.pid
118
+
119
+ # SageMath parsed files
120
+ *.sage.py
121
+
122
+ # Environments
123
+ .env
124
+ .venv
125
+ env/
126
+ venv/
127
+ ENV/
128
+ env.bak/
129
+ venv.bak/
130
+
131
+ # Spyder project settings
132
+ .spyderproject
133
+ .spyproject
134
+
135
+ # Rope project settings
136
+ .ropeproject
137
+
138
+ # mkdocs documentation
139
+ /site
140
+
141
+ # mypy
142
+ .mypy_cache/
143
+ .dmypy.json
144
+ dmypy.json
145
+
146
+ # Pyre type checker
147
+ .pyre/
148
+
149
+ # pytype static type analyzer
150
+ .pytype/
151
+
152
+ # Cython debug symbols
153
+ cython_debug/
154
+
155
+ # PyCharm
156
+ # JetBrains specific template is maintained in a separate JetBrains.gitignore that can
157
+ # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
158
+ # and can be added to the global gitignore or merged into this file. For a more nuclear
159
+ # option (not recommended) you can uncomment the following to ignore the entire idea folder.
160
+ #.idea/
161
+
162
+ .envrc
.python-version ADDED
@@ -0,0 +1 @@
 
 
1
+ 3.10.13
app.py ADDED
@@ -0,0 +1,135 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+
3
+ import dotenv
4
+ import gradio as gr # type: ignore
5
+ from mistralai.client import MistralClient # type: ignore
6
+ from mistralai.models.chat_completion import ChatMessage # type: ignore
7
+
8
+ dotenv.load_dotenv()
9
+
10
+
11
+ MISTRAL_API_KEY = os.environ.get("MISTRAL_API_KEY")
12
+
13
+
14
+ TITLE = """<h1 align="center">MistralAI Playground 💬</h1>"""
15
+ AVATAR_IMAGES = (None, "https://media.roboflow.com/spaces/gemini-icon.png")
16
+
17
+
18
+ chatbot_component = gr.Chatbot(
19
+ label="MistralAI", bubble_full_width=False, avatar_images=AVATAR_IMAGES, scale=2, height=400
20
+ )
21
+ text_prompt_component = gr.Textbox(placeholder="Hi there! [press Enter]", show_label=False, autofocus=True, scale=8)
22
+ run_button_component = gr.Button(value="Run", variant="primary", scale=1)
23
+ mistral_key_component = gr.Textbox(
24
+ label="MISTRAL API KEY",
25
+ value="",
26
+ type="password",
27
+ placeholder="...",
28
+ info="You have to provide your own MISTRAL_API_KEY for this app to function properly",
29
+ visible=MISTRAL_API_KEY is None,
30
+ )
31
+ model_component = gr.Dropdown(
32
+ choices=["mistral-tiny", "mistral-small", "mistral-medium"],
33
+ label="Model",
34
+ value="mistral-small",
35
+ scale=1,
36
+ type="value",
37
+ )
38
+ temperature_component = gr.Slider(
39
+ minimum=0,
40
+ maximum=1.0,
41
+ value=0.7,
42
+ step=0.05,
43
+ label="Temperature",
44
+ info=(
45
+ "What sampling temperature to use, between 0.0 and 1.0. "
46
+ "Higher values like 0.8 will make the output more random, while lower values like 0.2 will make it more focused and deterministic."
47
+ "We generally recommend altering this or top_p but not both."
48
+ ),
49
+ )
50
+
51
+ user_inputs = [
52
+ text_prompt_component,
53
+ chatbot_component,
54
+ ]
55
+ bot_inputs = [
56
+ mistral_key_component,
57
+ model_component,
58
+ temperature_component,
59
+ chatbot_component,
60
+ ]
61
+
62
+
63
+ client: MistralClient = None
64
+
65
+
66
+ def preprocess_chat_history(history):
67
+ chat_history = []
68
+ for human, assistant in history:
69
+ if human:
70
+ chat_history.append(ChatMessage(role="user", content=human))
71
+ if assistant:
72
+ chat_history.append(ChatMessage(role="assistant", content=assistant))
73
+ return chat_history
74
+
75
+
76
+ def bot(
77
+ mistral_key: str | None,
78
+ model: str,
79
+ temperature: float,
80
+ history,
81
+ ):
82
+ if not history:
83
+ return history
84
+
85
+ mistral_key = mistral_key or MISTRAL_API_KEY
86
+ if not mistral_key:
87
+ raise ValueError("MISTRAL_API_KEY is not set. Please follow the instructions in the README to set it up.")
88
+ global client
89
+ if client is None:
90
+ client = MistralClient(api_key=mistral_key) # TDOO: how to handle this if no GIL
91
+
92
+ chat_history = preprocess_chat_history(history)
93
+ history[-1][1] = ""
94
+ for chunk in client.chat_stream(model=model, messages=chat_history, temperature=temperature):
95
+ print("chunk", chunk)
96
+ if chunk.choices and chunk.choices[0].delta.content:
97
+ history[-1][1] += chunk.choices[0].delta.content
98
+ yield history
99
+
100
+
101
+ def user(text_prompt: str, history):
102
+ if text_prompt:
103
+ history.append((text_prompt, None))
104
+ return "", history
105
+
106
+
107
+ with gr.Blocks() as demo:
108
+ gr.HTML(TITLE)
109
+ with gr.Column():
110
+ mistral_key_component.render()
111
+ chatbot_component.render()
112
+ with gr.Row():
113
+ text_prompt_component.render()
114
+ run_button_component.render()
115
+ with gr.Accordion("Parameters", open=False):
116
+ model_component.render()
117
+ temperature_component.render()
118
+ run_button_component.click(
119
+ fn=user, inputs=user_inputs, outputs=[text_prompt_component, chatbot_component], queue=False
120
+ ).then(
121
+ fn=bot,
122
+ inputs=bot_inputs,
123
+ outputs=[chatbot_component],
124
+ )
125
+
126
+ text_prompt_component.submit(
127
+ fn=user, inputs=user_inputs, outputs=[text_prompt_component, chatbot_component], queue=False
128
+ ).then(
129
+ fn=bot,
130
+ inputs=bot_inputs,
131
+ outputs=[chatbot_component],
132
+ )
133
+
134
+
135
+ demo.queue(max_size=99).launch(debug=False, show_error=True)
env.example ADDED
@@ -0,0 +1 @@
 
 
1
+ MISTRAL_API_KEY="<your-mistral-api-key>"
requirements-dev.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ mistralai==0.0.8
2
+ gradio
3
+ python-dotenv
4
+ mypy
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ mistralai==0.0.8
2
+ gradio
3
+ python-dotenv