commonlemon commited on
Commit
e238a65
·
verified ·
1 Parent(s): 1baa139

1st commit lesson 11

Browse files
Files changed (1) hide show
  1. app.py +22 -4
app.py CHANGED
@@ -1,14 +1,32 @@
1
- import gradio as gr, random
 
2
 
 
 
3
 
4
- def echo(message, history): #function for Gradio to call
 
5
  #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
6
  #I have to put it in this order because Gradio will always past the current user input first and then the convo history
7
  # however for now, this chatbot won't use the history parameter anyway
8
- return random.choice(["Yes", "No", "Maybe"])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
 
10
  # defining chatbot
11
- chatbot = gr.ChatInterface(echo, title = "magic 8 ball", description = " user asks a yes or no question to the ball to reveal a random answer") #using gradio to quickly build a chatbot UI (w/ convo history & user input)
12
  # passing fxn into a fxn, passing echo for gradio to call each time the user sends a message
13
  # 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
14
 
 
1
+ import gradio as gr
2
+ from huggingface_hub import InferenceClient #InferenceClient class
3
 
4
+ client = InferenceClient("Qwen/Qwen2.5-7B-Instruct") #Create an instance of InferenceClient connected to the Qwen/Qwen2.5-7B-Instruct text-generation model
5
+ #this client will handle making requests to the model to generate responses
6
 
7
+
8
+ def respond(message, history): #function for Gradio to call
9
  #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
10
  #I have to put it in this order because Gradio will always past the current user input first and then the convo history
11
  # however for now, this chatbot won't use the history parameter anyway
12
+ messages = [{"role": "system", "content": "You are a friendly chatbot."}] #dict in list to store messages
13
+
14
+ #Add convo history to the messages if there's convo history
15
+ if history:
16
+ messages.extend(history)
17
+
18
+ messages.append({"role": "user", "content": message}) #add the current user’s message to the messages list
19
+
20
+ # chat completion API call forwarding the messages & other params to model
21
+ response = client.chat_completion(
22
+ messages,
23
+ max_tokens=100
24
+ )
25
+
26
+ return response.choices[0].message.content.strip()
27
 
28
  # defining chatbot
29
+ chatbot = gr.ChatInterface(respond, title = "", description = "") #using gradio to quickly build a chatbot UI (w/ convo history & user input)
30
  # passing fxn into a fxn, passing echo for gradio to call each time the user sends a message
31
  # 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
32