naelghouti commited on
Commit
2e9a40d
Β·
verified Β·
1 Parent(s): e0bf0ec

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -45
app.py CHANGED
@@ -1,42 +1,16 @@
1
- # app.py
2
-
3
- # ── 0) Monkey-patch DNS for the HF Inference API hostname ─────────────────────
4
- import socket
5
-
6
- IP_POOL = [
7
- "3.165.206.104",
8
- "3.165.206.54",
9
- "3.165.206.39",
10
- "3.165.206.82",
11
- ]
12
- _orig_getaddrinfo = socket.getaddrinfo
13
-
14
- def patched_getaddrinfo(host, port, family=0, socktype=0, proto=0, flags=0):
15
- if host == "api-inference.huggingface.co":
16
- return [
17
- (family or socket.AF_INET,
18
- socktype or socket.SOCK_STREAM,
19
- proto or socket.IPPROTO_TCP,
20
- "",
21
- (ip, port))
22
- for ip in IP_POOL
23
- ]
24
- return _orig_getaddrinfo(host, port, family, socktype, proto, flags)
25
-
26
- socket.getaddrinfo = patched_getaddrinfo
27
-
28
- # ── 1) Imports & Client setup ─────────────────────────────────────────────────
29
- import os
30
  import gradio as gr
31
  from huggingface_hub import InferenceClient
 
 
 
 
 
 
32
 
33
- # Ensure you have set HF_TOKEN in your Space’s Secrets
34
- client = InferenceClient(
35
- model="HuggingFaceH4/zephyr-7b-beta",
36
- token=os.environ["HF_TOKEN"]
37
- )
38
 
39
- # ── 2) One-shot chat function ─────────────────────────────────────────────────
40
  def respond(
41
  system_message: str,
42
  user_prompt: str,
@@ -44,6 +18,7 @@ def respond(
44
  temperature: float,
45
  top_p: float
46
  ) -> str:
 
47
  messages = [
48
  {"role": "system", "content": system_message},
49
  {"role": "user", "content": user_prompt}
@@ -53,19 +28,18 @@ def respond(
53
  max_tokens=max_tokens,
54
  temperature=temperature,
55
  top_p=top_p,
56
- stream=False
57
  )
 
 
 
58
  return resp.choices[0].message.content.strip()
59
 
60
- # ── 3) Gradio Interface with Queue ────────────────────────────────────────────
61
  demo = gr.Interface(
62
  fn=respond,
63
  inputs=[
64
- gr.Textbox(
65
- "You are a helpful and friendly assistant for children. "
66
- "All responses must be safe and positive.",
67
- label="System message"
68
- ),
69
  gr.Textbox(placeholder="Type your question…", label="User prompt"),
70
  gr.Slider(1, 2048, 512, label="Max tokens"),
71
  gr.Slider(0.1, 4.0, 0.7, label="Temperature"),
@@ -76,8 +50,7 @@ demo = gr.Interface(
76
  flagging_mode="never",
77
  )
78
 
79
- # Enable the REST API on /api/queue/predict
80
-
81
- # ── 4) Launch ────────────────────────────────────────────────────────────────
82
  if __name__ == "__main__":
83
  demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
3
+ import socket
4
+ try:
5
+ print("Resolving api-inference.huggingface.co...")
6
+ print(socket.gethostbyname("api-inference.huggingface.co"))
7
+ except Exception as e:
8
+ print("DNS failed:", e)
9
 
10
+ # 1) Initialize your HF client
11
+ client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
 
 
 
12
 
13
+ # 2) A pure, one-shot function
14
  def respond(
15
  system_message: str,
16
  user_prompt: str,
 
18
  temperature: float,
19
  top_p: float
20
  ) -> str:
21
+
22
  messages = [
23
  {"role": "system", "content": system_message},
24
  {"role": "user", "content": user_prompt}
 
28
  max_tokens=max_tokens,
29
  temperature=temperature,
30
  top_p=top_p,
31
+ stream=False # <-- non-streaming
32
  )
33
+
34
+
35
+
36
  return resp.choices[0].message.content.strip()
37
 
38
+ # 3) Create a non-streaming Interface
39
  demo = gr.Interface(
40
  fn=respond,
41
  inputs=[
42
+ gr.Textbox("You are a helpful and friendly assistant for children.", label="System message"),
 
 
 
 
43
  gr.Textbox(placeholder="Type your question…", label="User prompt"),
44
  gr.Slider(1, 2048, 512, label="Max tokens"),
45
  gr.Slider(0.1, 4.0, 0.7, label="Temperature"),
 
50
  flagging_mode="never",
51
  )
52
 
53
+ # 4) Enable the REST API and then launch
54
+ demo.queue(api_open=True)
 
55
  if __name__ == "__main__":
56
  demo.launch()