init space
Browse files- app.py +64 -0
- requirements.txt +1 -0
app.py
ADDED
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import gradio as gr
|
3 |
+
from mistralai.client import MistralClient
|
4 |
+
from mistralai.models.chat_completion import ChatMessage
|
5 |
+
|
6 |
+
api_key = os.environ["MISTRAL_API_KEY"]
|
7 |
+
model = "mistral-large-latest"
|
8 |
+
|
9 |
+
client = MistralClient(api_key=api_key)
|
10 |
+
|
11 |
+
header = r"""
|
12 |
+
<center>
|
13 |
+
<h1>Mistral Chat</h1>
|
14 |
+
<h2>
|
15 |
+
This demo is for code reference, feel free to clone it and set your own Api key
|
16 |
+
</h2>
|
17 |
+
Get an Api Key: <a href='https://console.mistral.ai/api-keys'>Mistral Ai</a>
|
18 |
+
</center>
|
19 |
+
"""
|
20 |
+
|
21 |
+
footer = r"""
|
22 |
+
<center>
|
23 |
+
<b>
|
24 |
+
Demo for <a href='https://mistral.ai'>Mistral AI</a>
|
25 |
+
</b>
|
26 |
+
</center>
|
27 |
+
"""
|
28 |
+
|
29 |
+
with gr.Blocks(title="Mistral Chat") as app:
|
30 |
+
|
31 |
+
gr.HTML(header)
|
32 |
+
|
33 |
+
with gr.Row(equal_height=True):
|
34 |
+
with gr.Column():
|
35 |
+
chatbot = gr.Chatbot()
|
36 |
+
msg = gr.Textbox()
|
37 |
+
clear = gr.Button("Clear")
|
38 |
+
|
39 |
+
def user(user_message, history):
|
40 |
+
return "", history + [[user_message, None]]
|
41 |
+
|
42 |
+
def bot(history):
|
43 |
+
|
44 |
+
messages = [
|
45 |
+
ChatMessage(role="user", content=history[-1][0])
|
46 |
+
]
|
47 |
+
|
48 |
+
response = client.chat_stream(model=model, messages=messages)
|
49 |
+
|
50 |
+
history[-1][1] = ""
|
51 |
+
for chunk in response:
|
52 |
+
history[-1][1] += chunk.choices[0].delta.content
|
53 |
+
yield history
|
54 |
+
|
55 |
+
msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
|
56 |
+
bot, chatbot, chatbot
|
57 |
+
)
|
58 |
+
clear.click(lambda: None, None, chatbot, queue=False)
|
59 |
+
|
60 |
+
with gr.Row():
|
61 |
+
gr.HTML(footer)
|
62 |
+
|
63 |
+
app.queue()
|
64 |
+
app.launch(share=False, debug=True, show_error=True)
|
requirements.txt
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
mistralai
|