Rahatara commited on
Commit
72c2541
·
verified ·
1 Parent(s): fcda40d

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +79 -0
app.py ADDED
@@ -0,0 +1,79 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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:
6
+ https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
7
+ """
8
+
9
+ # Initialize the Hugging Face inference client
10
+ client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
11
+
12
+
13
+ def respond(
14
+ message,
15
+ history: list[tuple[str, str]],
16
+ system_message,
17
+ max_tokens,
18
+ temperature,
19
+ top_p,
20
+ ):
21
+ # Prepare the messages in the required format
22
+ messages = [{"role": "system", "content": system_message}]
23
+
24
+ for val in history:
25
+ if val[0]: # User message
26
+ messages.append({"role": "user", "content": val[0]})
27
+ if val[1]: # Assistant response
28
+ messages.append({"role": "assistant", "content": val[1]})
29
+
30
+ # Append the latest user message
31
+ messages.append({"role": "user", "content": message})
32
+
33
+ # Ensure the structure is correct before sending to the API
34
+ try:
35
+ response = ""
36
+ for msg in client.chat_completion(
37
+ messages=messages,
38
+ max_tokens=max_tokens,
39
+ stream=True,
40
+ temperature=temperature,
41
+ top_p=top_p,
42
+ ):
43
+ token = msg["choices"][0]["delta"]["content"]
44
+ response += token
45
+ yield response
46
+ except Exception as e:
47
+ yield f"An error occurred: {e}"
48
+
49
+
50
+ """
51
+ For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
52
+ """
53
+ demo = gr.ChatInterface(
54
+ respond,
55
+ additional_inputs=[
56
+ gr.Textbox(
57
+ value="You are an expert mental health counselor. You guide and help people to relax and calm down.",
58
+ label="System message",
59
+ ),
60
+ gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
61
+ gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
62
+ gr.Slider(
63
+ minimum=0.1,
64
+ maximum=1.0,
65
+ value=0.95,
66
+ step=0.05,
67
+ label="Top-p (nucleus sampling)",
68
+ ),
69
+ ],
70
+ examples=[
71
+ ["I feel overwhelmed with work."],
72
+ ["Can you guide me through a quick meditation?"],
73
+ ["How do I stop worrying about things I can't control?"],
74
+ ],
75
+ title="Calm Mate 🕊️",
76
+ )
77
+
78
+ if __name__ == "__main__":
79
+ demo.launch()