mayankchugh-learning commited on
Commit
548b4c3
·
verified ·
1 Parent(s): 7b5e06d

Fix Space runtime: update SDK/deps and harden startup

Browse files
Files changed (3) hide show
  1. README.md +13 -12
  2. app.py +45 -71
  3. requirements.txt +2 -2
README.md CHANGED
@@ -1,13 +1,14 @@
1
- ---
2
- title: AgeBot
3
- emoji: 💬
4
- colorFrom: yellow
5
- colorTo: purple
6
- sdk: gradio
7
- sdk_version: 4.36.1
8
- app_file: app.py
9
- pinned: false
10
- license: mit
11
- ---
12
-
 
13
  An example chatbot using [Gradio](https://gradio.app), [`huggingface_hub`](https://huggingface.co/docs/huggingface_hub/v0.22.2/en/index), and the [Hugging Face Inference API](https://huggingface.co/docs/api-inference/index).
 
1
+ ---
2
+ title: AgeBot
3
+ emoji: 💬
4
+ colorFrom: yellow
5
+ colorTo: purple
6
+ sdk: gradio
7
+ sdk_version: 4.44.1
8
+ python_version: 3.10
9
+ app_file: app.py
10
+ pinned: false
11
+ license: mit
12
+ ---
13
+
14
  An example chatbot using [Gradio](https://gradio.app), [`huggingface_hub`](https://huggingface.co/docs/huggingface_hub/v0.22.2/en/index), and the [Hugging Face Inference API](https://huggingface.co/docs/api-inference/index).
app.py CHANGED
@@ -1,71 +1,45 @@
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
-
8
- client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
9
- # client = InferenceClient("meta-llama/Meta-Llama-3-8B")
10
-
11
- # Define the prompt and system message
12
- prompt = "Enter the age you want the bot to be as n = _ and choose sex as male or female. Example n = 17 and sex = male"
13
- system_message = "You are an assistant bot who speaks based on given age and sex. Enter the age you want the bot to be as n = _ and choose sex as male or female. Example n = 17 and sex = male"
14
-
15
- # Generate the response
16
- # response = model(f"[INST] {system_message} {prompt} [/INST]")
17
-
18
- def respond(
19
- message,
20
- history: list[tuple[str, str]],
21
- system_message,
22
- max_tokens,
23
- temperature,
24
- top_p,
25
- ):
26
- messages = [{"role": "system", "content": system_message, "message": prompt}]
27
-
28
- for val in history:
29
- if val[0]:
30
- messages.append({"role": "user", "content": val[0]})
31
- if val[1]:
32
- messages.append({"role": "assistant", "content": val[1]})
33
-
34
- messages.append({"role": "user", "content": message})
35
-
36
- response = ""
37
-
38
- for message in client.chat_completion(
39
- messages,
40
- max_tokens=max_tokens,
41
- stream=True,
42
- temperature=temperature,
43
- top_p=top_p,
44
- ):
45
- token = message.choices[0].delta.content
46
-
47
- response += token
48
- yield response
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(value="You are a friendly Chatbot. Enter the age you want the bot to be as n = _ and choose sex as male or female. Example n = 17 and sex = male", label="System message"),
57
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
58
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
59
- gr.Slider(
60
- minimum=0.1,
61
- maximum=1.0,
62
- value=0.95,
63
- step=0.05,
64
- label="Top-p (nucleus sampling)",
65
- ),
66
- ],
67
- )
68
-
69
-
70
- if __name__ == "__main__":
71
- demo.launch()
 
1
+ import gradio as gr
2
+ from huggingface_hub import InferenceClient
3
+
4
+ client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
5
+
6
+ def respond(message, history, system_message, max_tokens, temperature, top_p):
7
+ messages = [{"role": "system", "content": system_message}]
8
+ for user_msg, assistant_msg in history:
9
+ if user_msg:
10
+ messages.append({"role": "user", "content": user_msg})
11
+ if assistant_msg:
12
+ messages.append({"role": "assistant", "content": assistant_msg})
13
+ messages.append({"role": "user", "content": message})
14
+
15
+ response = ""
16
+ try:
17
+ for chunk in client.chat_completion(
18
+ messages,
19
+ max_tokens=int(max_tokens),
20
+ stream=True,
21
+ temperature=temperature,
22
+ top_p=top_p,
23
+ ):
24
+ token = chunk.choices[0].delta.content
25
+ if token:
26
+ response += token
27
+ yield response
28
+ except Exception as exc:
29
+ yield f"Model call failed: {exc}. Add HF_TOKEN in Space secrets if needed."
30
+
31
+ demo = gr.ChatInterface(
32
+ respond,
33
+ additional_inputs=[
34
+ gr.Textbox(
35
+ value="You are a friendly chatbot. Speak as if you are age n and a given sex. Example: n = 17 and sex = male",
36
+ label="System message",
37
+ ),
38
+ gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
39
+ gr.Slider(minimum=0.1, maximum=2.0, value=0.7, step=0.1, label="Temperature"),
40
+ gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p"),
41
+ ],
42
+ )
43
+
44
+ if __name__ == "__main__":
45
+ demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
requirements.txt CHANGED
@@ -1,2 +1,2 @@
1
- huggingface_hub==0.22.2
2
- minijinja
 
1
+ gradio==4.44.1
2
+ huggingface_hub>=0.26.0