Kkordik commited on
Commit
f48ca32
·
verified ·
1 Parent(s): 5dd93fa

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -0
app.py ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from huggingface_hub import snapshot_download
3
+ from pathlib import Path
4
+ from mistral.cli.chat import load_model, generate_stream
5
+
6
+ # Download the model
7
+ mistral_models_path = Path.home().joinpath('mistral_models', 'mamba-codestral-7B-v0.1')
8
+ mistral_models_path.mkdir(parents=True, exist_ok=True)
9
+
10
+ snapshot_download(repo_id="mistralai/mamba-codestral-7B-v0.1",
11
+ allow_patterns=["params.json", "consolidated.safetensors", "tokenizer.model.v3"],
12
+ local_dir=mistral_models_path)
13
+
14
+ # Load the model
15
+ model = load_model(str(mistral_models_path))
16
+
17
+ def generate_response(message, history):
18
+ history_mistral_format = [
19
+ {"role": "user" if i % 2 == 0 else "assistant", "content": m}
20
+ for i, m in enumerate(sum(history, []))
21
+ ]
22
+ history_mistral_format.append({"role": "user", "content": message})
23
+
24
+ response = ""
25
+ for chunk in generate_stream(model, history_mistral_format, max_tokens=256):
26
+ response += chunk
27
+ yield response
28
+
29
+ iface = gr.ChatInterface(
30
+ generate_response,
31
+ title="Mamba Codestral Chat",
32
+ description="Chat with the Mamba Codestral 7B model.",
33
+ )
34
+
35
+ if __name__ == "__main__":
36
+ iface.launch()