Arnic commited on
Commit
7423722
·
verified ·
1 Parent(s): a5f7ed0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -31
app.py CHANGED
@@ -1,12 +1,10 @@
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
3
 
4
- """
5
- 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
6
- """
7
  client = InferenceClient("Arnic/gemma2-2b-it-Pubmed20k-TPU")
8
 
9
-
10
  def respond(
11
  message,
12
  history: list[tuple[str, str]],
@@ -15,35 +13,35 @@ def respond(
15
  temperature,
16
  top_p,
17
  ):
18
- system_message = " You are a good listener. You advise relaxation exercises, suggest avoiding negative thoughts, and guide through steps to manage stress. Let's discuss what's on your mind, or ask me for a quick relaxation exercise."
19
- messages = [{"role": "system", "content": system_message}]
20
-
21
- for val in history:
22
- if val[0]:
23
- messages.append({"role": "user", "content": val[0]})
24
- if val[1]:
25
- messages.append({"role": "assistant", "content": val[1]})
26
-
27
- messages.append({"role": "user", "content": message})
28
-
29
- response = ""
30
-
31
- for message in client.chat_completion(
32
- messages,
33
- max_tokens=max_tokens,
34
- stream=True,
 
 
 
35
  temperature=temperature,
36
- top_p=top_p,
37
- ):
38
- token = message.choices[0].delta.content
39
-
40
- response += token
41
- yield response
42
 
 
 
 
43
 
44
- """
45
- For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
46
- """
47
  demo = gr.ChatInterface(
48
  respond,
49
  additional_inputs=[
@@ -60,6 +58,5 @@ demo = gr.ChatInterface(
60
  ],
61
  )
62
 
63
-
64
  if __name__ == "__main__":
65
  demo.launch()
 
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
3
 
4
+ # Initialize the client with your model
 
 
5
  client = InferenceClient("Arnic/gemma2-2b-it-Pubmed20k-TPU")
6
 
7
+ # Define response function
8
  def respond(
9
  message,
10
  history: list[tuple[str, str]],
 
13
  temperature,
14
  top_p,
15
  ):
16
+ system_message = (
17
+ "You are a good listener. You advise relaxation exercises, suggest avoiding negative thoughts, "
18
+ "and guide through steps to manage stress. Let's discuss what's on your mind, "
19
+ "or ask me for a quick relaxation exercise."
20
+ )
21
+
22
+ # Format history and system message as prompt text
23
+ chat_history = ""
24
+ for user_msg, bot_reply in history:
25
+ if user_msg:
26
+ chat_history += f"User: {user_msg}\n"
27
+ if bot_reply:
28
+ chat_history += f"Assistant: {bot_reply}\n"
29
+
30
+ prompt = f"{system_message}\n\n{chat_history}User: {message}\nAssistant:"
31
+
32
+ # Generate response using the InferenceClient text generation method
33
+ response = client.text_generation(
34
+ prompt=prompt,
35
+ max_new_tokens=max_tokens,
36
  temperature=temperature,
37
+ top_p=top_p
38
+ )
 
 
 
 
39
 
40
+ # Extract and yield the text response
41
+ generated_text = response["generated_text"].replace(prompt, "").strip()
42
+ yield generated_text
43
 
44
+ # Set up Gradio interface
 
 
45
  demo = gr.ChatInterface(
46
  respond,
47
  additional_inputs=[
 
58
  ],
59
  )
60
 
 
61
  if __name__ == "__main__":
62
  demo.launch()