ChatbotLab-IA / app.py
commonlemon's picture
Update app.py
2861dbc verified
import gradio as gr
from huggingface_hub import InferenceClient #InferenceClient class
client = InferenceClient("deepseek-ai/DeepSeek-R1-Distill-Qwen-32B") #Create an instance of InferenceClient connected to the Qwen/Qwen2.5-7B-Instruct text-generation model
#this client will handle making requests to the model to generate responses
def respond(message, history): #function for Gradio to call
#Gradio passes arguments as parameters: the user's most recent input which is a string ("message"), and "history" which is the list of past messages
#I have to put it in this order because Gradio will always past the current user input first and then the convo history
# however for now, this chatbot won't use the history parameter anyway
messages = [{"role": "system", "content": "You are a friendly chatbot."}] #dict in list to store messages
#Add convo history to the messages if there's convo history
if history:
messages.extend(history)
messages.append({"role": "user", "content": message}) #add the current user’s message to the messages list
# chat completion API call forwarding the messages & other params to model
response = client.chat_completion(messages, max_tokens=100, temperature = 2, top_p=0.95) #deepseek R1 recomended temp range: 0.5-0.7
return response.choices[0].message.content.strip()
# defining chatbot
chatbot = gr.ChatInterface(respond, title = "", description = "") #using gradio to quickly build a chatbot UI (w/ convo history & user input)
# passing fxn into a fxn, passing echo for gradio to call each time the user sends a message
# Adding parentheses would call the function and pass its return value instead, I didn't include () because I want Gradio to call it later, not right now
chatbot.launch() #launch chatbot