ashimanair commited on
Commit
4092c47
·
verified ·
1 Parent(s): 08d682d

practice for lesson 11 generative AI -----> adding the respond function + important InferenceClient (from hugging face)

Browse files
Files changed (1) hide show
  1. app.py +15 -1
app.py CHANGED
@@ -1,11 +1,25 @@
1
  import gradio as gr
2
  import random
 
3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
  def random_message(message, history):
5
  choices = ["try again later", "isn't looking good for you", "perhaps", "maybe", "doubtful", "yes", "no", "without a doubt", "most likely", "signs point to yes", "it is certain", "my sources say no", "reply hazy, try again"]
6
  random_choices = random.choice(choices)
7
  return random_choices
8
 
9
- chatbot = gr.ChatInterface(random_message, type = "messages", title = "Welcome to 8 Ball")
10
 
11
  chatbot.launch()
 
1
  import gradio as gr
2
  import random
3
+ from huggingface_hub import InferenceClient
4
 
5
+ client = InferenceClient ("HuggingFaceH4/zephyr-7b-beta")
6
+
7
+ def respond(message, history):
8
+ messages = [{"role" : "system", "content" : "You are a friendly chatbot."}]
9
+ if history:
10
+ messages.extend(history)
11
+
12
+ messages.append({"role" : "user", "content" : message})
13
+ response = client.chat_completion(messages, max_tokens = 100)
14
+
15
+ print(response["choices"][0]["message"]["content"].strip())
16
+ return response["choices"][0]["message"]["content"].strip()
17
+
18
  def random_message(message, history):
19
  choices = ["try again later", "isn't looking good for you", "perhaps", "maybe", "doubtful", "yes", "no", "without a doubt", "most likely", "signs point to yes", "it is certain", "my sources say no", "reply hazy, try again"]
20
  random_choices = random.choice(choices)
21
  return random_choices
22
 
23
+ chatbot = gr.ChatInterface(respond, type = "messages")
24
 
25
  chatbot.launch()