Smarter_Chatbot / app.py
praveenparimi's picture
Update app.py
6fde835 verified
import gradio as gr
from huggingface_hub import InferenceClient
from transformers import pipeline
"""
For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
"""
print("starting...")
# model_name = "microsoft/DialoGPT-medium" # Works, but isn't very good
model_name = "microsoft/Phi-3.5-mini-instruct"
chat_model = pipeline("text-generation", model=model_name)
print("defining function")
def respond(
message,
history: list[tuple[str, str]],
system_message,
max_tokens
):
"""The respond method is the main method in the chatbot. It is called when the user hits the enter key."""
print("enter respond")
messages = [{"role": "system", "content": system_message}]
messages.append({"role": "user", "content": message})
print("getting response",messages)
response = chat_model(messages)
print("got response",response)
return response[-1]['generated_text'][-1]['content']
"""
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
"""
demo = gr.ChatInterface(
respond,
additional_inputs=[
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens")
],
)
if __name__ == "__main__":
demo.launch()