-
Notifications
You must be signed in to change notification settings - Fork 89
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
uralik
wants to merge
1
commit into
main
Choose a base branch
from
kulikov/mistral_gradio
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
# Copyright (c) Meta Platforms, Inc. and affiliates. | ||
# 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 | ||
except ImportError: | ||
print("Install gradio: pip install 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 GitHub Actions / Lint Python / Lint
|
||
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() |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 ?