Spaces:
Sleeping
Sleeping
File size: 2,089 Bytes
e238a65 66d76cd 152ab18 e238a65 5367ce5 e238a65 1baa139 5367ce5 1baa139 8e89158 e238a65 8e89158 e238a65 2861dbc e238a65 86c4fa1 ecfc0b7 e238a65 0216182 d2582d3 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | 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) # adds history to the end of messages list via .extend() method
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()
# max_tokens, to limit the number of tokens that can be generated
# temperature, which controls randomness (higher = more random)
# top_p, an alternative to sampling with temperature
# 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 |