Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gradio interface for mistral chatbot #237

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions recipes/mistral/gradio_chatbot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# Copyright (c) Meta Platforms, Inc. and affiliates.

Check failure on line 1 in recipes/mistral/gradio_chatbot.py

View workflow job for this annotation

GitHub Actions / Lint Python / Lint

Imports are incorrectly sorted and/or formatted.
# All rights reserved.
#
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree.

try:
import gradio as gr

Check failure on line 8 in recipes/mistral/gradio_chatbot.py

View workflow job for this annotation

GitHub Actions / Lint Python / Lint

Cannot find implementation or library stub for module named "gradio"
except ImportError:
print("Install gradio: pip install gradio")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we specify the version of gradio ?


import torch

from fairseq2.assets import asset_store
from fairseq2.generation import (
ChatMessage,
SamplingSequenceGenerator,
TopPSampler,
)
from fairseq2.models.mistral import (
MistralChatbot,
load_mistral_model,
load_mistral_tokenizer,
)

model_card = asset_store.retrieve_card("mistral_7b_instruct")
model = load_mistral_model(
model_card, dtype=torch.float16, device=torch.device("cuda:0")
)
tokenizer = load_mistral_tokenizer(model_card)


def interact_with_chatbot(input_text: str, history: list, top_p: float):

Check failure on line 33 in recipes/mistral/gradio_chatbot.py

View workflow job for this annotation

GitHub Actions / Lint Python / Lint

Function is missing a return type annotation

Check failure on line 33 in recipes/mistral/gradio_chatbot.py

View workflow job for this annotation

GitHub Actions / Lint Python / Lint

Implicit generic "Any". Use "typing.List" and specify generic parameters
sampler = TopPSampler(p=top_p)
generator = SamplingSequenceGenerator(
model, sampler, temperature=1.0, max_gen_len=1024
)
chatbot = MistralChatbot(generator, tokenizer)

mistral_format_history = []
for user, bot in history:
user_msg = ChatMessage(role="user", content=user)
bot_msg = ChatMessage(role="bot", content=bot)
mistral_format_history.extend([user_msg, bot_msg])

mistral_format_history.append(ChatMessage(role="user", content=input_text))
response, _ = chatbot(mistral_format_history)

return str(response.content)


demo = gr.ChatInterface(
interact_with_chatbot,
chatbot=gr.Chatbot(height=600),
textbox=gr.Textbox(placeholder="Type your prompt here", container=False, scale=7),
additional_inputs=[gr.Slider(0.1, 1.0, value=0.9, step=0.01, label="nucleus topp")],
title="Mistral Instruct 7B",
description="Mistral Instruct 7B served locally by fairseq2",
theme="soft",
retry_btn=None,
undo_btn="Delete Previous",
clear_btn="Clear",
)


if __name__ == "__main__":
demo.launch()
Loading