Spaces:
Running
on
Zero
Running
on
Zero
First commit
Browse files- README.md +4 -2
- app.py +59 -0
- requirements.txt +3 -0
README.md
CHANGED
|
@@ -4,10 +4,12 @@ emoji: 🐨
|
|
| 4 |
colorFrom: gray
|
| 5 |
colorTo: yellow
|
| 6 |
sdk: gradio
|
| 7 |
-
sdk_version:
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
-
|
|
|
|
|
|
|
| 11 |
---
|
| 12 |
|
| 13 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
|
| 4 |
colorFrom: gray
|
| 5 |
colorTo: yellow
|
| 6 |
sdk: gradio
|
| 7 |
+
sdk_version: 4.44.0
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
+
models:
|
| 11 |
+
- mistralai/Pixtral-12B-2409
|
| 12 |
+
short_description: Chat with Pixtral 12B Multimodal using Mistral Inference
|
| 13 |
---
|
| 14 |
|
| 15 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
app.py
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from gradio.data_classes import FileData
|
| 3 |
+
from huggingface_hub import snapshot_download
|
| 4 |
+
from pathlib import Path
|
| 5 |
+
import base64
|
| 6 |
+
import spaces
|
| 7 |
+
import os
|
| 8 |
+
|
| 9 |
+
from mistral_inference.transformer import Transformer
|
| 10 |
+
from mistral_inference.generate import generate
|
| 11 |
+
|
| 12 |
+
from mistral_common.tokens.tokenizers.mistral import MistralTokenizer
|
| 13 |
+
from mistral_common.protocol.instruct.messages import UserMessage, AssistantMessage, TextChunk, ImageURLChunk
|
| 14 |
+
from mistral_common.protocol.instruct.request import ChatCompletionRequest
|
| 15 |
+
|
| 16 |
+
models_path = Path.home().joinpath('pixtral', 'Pixtral')
|
| 17 |
+
models_path.mkdir(parents=True, exist_ok=True)
|
| 18 |
+
|
| 19 |
+
snapshot_download(repo_id="mistral-community/pixtral-12b-240910",
|
| 20 |
+
allow_patterns=["params.json", "consolidated.safetensors", "tekken.json"],
|
| 21 |
+
local_dir=models_path)
|
| 22 |
+
|
| 23 |
+
tokenizer = MistralTokenizer.from_file(f"{models_path}/tekken.json")
|
| 24 |
+
model = Transformer.from_folder(models_path)
|
| 25 |
+
|
| 26 |
+
def image_to_base64(image_path):
|
| 27 |
+
with open(image_path, 'rb') as img:
|
| 28 |
+
encoded_string = base64.b64encode(img.read()).decode('utf-8')
|
| 29 |
+
return f"data:image/jpeg;base64,{encoded_string}"
|
| 30 |
+
|
| 31 |
+
@spaces.GPU(duration=60)
|
| 32 |
+
def run_inference(message, history):
|
| 33 |
+
## may work
|
| 34 |
+
messages = []
|
| 35 |
+
images = []
|
| 36 |
+
for couple in history:
|
| 37 |
+
if type(couple[0]) is tuple:
|
| 38 |
+
images += couple[0]
|
| 39 |
+
elif couple[0][1]:
|
| 40 |
+
messages.append(UserMessage(content = [ImageURLChunk(image_url=image_to_base64(path)) for path in images]+[TextChunk(text=couple[0][1])]))
|
| 41 |
+
messages.append(AssistantMessage(content = couple[1]))
|
| 42 |
+
images = []
|
| 43 |
+
##
|
| 44 |
+
|
| 45 |
+
messages.append(UserMessage(content = [ImageURLChunk(image_url=image_to_base64(file["path"])) for file in message["files"]]+[TextChunk(text=message["text"])]))
|
| 46 |
+
|
| 47 |
+
completion_request = ChatCompletionRequest(messages=messages)
|
| 48 |
+
|
| 49 |
+
encoded = tokenizer.encode_chat_completion(completion_request)
|
| 50 |
+
|
| 51 |
+
images = encoded.images
|
| 52 |
+
tokens = encoded.tokens
|
| 53 |
+
|
| 54 |
+
out_tokens, _ = generate([tokens], model, images=[images], max_tokens=512, temperature=0.45, eos_id=tokenizer.instruct_tokenizer.tokenizer.eos_id)
|
| 55 |
+
result = tokenizer.decode(out_tokens[0])
|
| 56 |
+
return result
|
| 57 |
+
|
| 58 |
+
demo = gr.ChatInterface(fn=run_inference, title="Pixtral 12B", multimodal=True, description="A demo chat interface with Pixtral 12B, deployed using Mistral Inference.")
|
| 59 |
+
demo.queue().launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
mistral_inference==1.4.0
|
| 2 |
+
huggingface_hub
|
| 3 |
+
gradio
|