hannantoprak commited on
Commit
1e981c6
Β·
verified Β·
1 Parent(s): 6f06c38

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -16
app.py CHANGED
@@ -6,7 +6,7 @@ from urllib3.exceptions import InsecureRequestWarning
6
  import requests
7
  from huggingface_hub import InferenceClient
8
 
9
- # ── 0) Monkey‑patch DNS for the HF Inference API hostname ────────────────────
10
  IP_POOL = [
11
  "3.165.206.104",
12
  "3.165.206.54",
@@ -27,7 +27,7 @@ def patched_getaddrinfo(host, port, family=0, socktype=0, proto=0, flags=0):
27
  return _orig_getaddrinfo(host, port, family, socktype, proto, flags)
28
  socket.getaddrinfo = patched_getaddrinfo
29
 
30
- # ── 1) Sanity‑check DNS + HTTP for example.com ─────────────────────────────────
31
  try:
32
  ip = socket.gethostbyname("example.com")
33
  print("βœ… example.com β†’", ip)
@@ -43,13 +43,12 @@ except Exception as e:
43
  HF_TOKEN = os.environ["HF_TOKEN"]
44
  print("πŸ”‘ HF_TOKEN present?", bool(HF_TOKEN))
45
 
46
- # ── CORRECTED: pass the model as positional or with the 'model=' keyword ─────
47
  client = InferenceClient(
48
- model="HuggingFaceH4/zephyr-7b-beta", # ← use 'model', not 'repo_id'
49
- token=HF_TOKEN # ← or api_key=HF_TOKEN
50
  )
51
 
52
- # ── 3) One‑shot chat function ─────────────────────────────────────────────────
53
  def respond(
54
  system_message: str,
55
  user_prompt: str,
@@ -68,28 +67,27 @@ def respond(
68
  top_p=top_p,
69
  stream=False
70
  )
71
- chat_response = resp.choices[0].message.content.strip()
72
 
73
- # Format the response to make it appear more like a chat
74
- formatted_response = f"Assistant: {chat_response}"
75
- return formatted_response
76
-
77
- # ── 4) Gradio Interface ────────────────────────────────────────────────────────
78
  demo = gr.Interface(
79
  fn=respond,
80
  inputs=[
81
- gr.Textbox("You are a helpful and friendly assistant for children. Your capabilities include answering questions about children's books, characters, and events, and generating engaging, imaginative stories. It is essential that all your responses and generated stories are safe, appropriate, and positive for children. If a question or request is inappropriate, or if it is not about children's books or generating a safe story, please politely explain that you can only help with safe requests related to children's books or stories.", label="System message"),
 
 
 
 
82
  gr.Textbox(placeholder="Type your question…", label="User prompt"),
83
  gr.Slider(1, 2048, 512, label="Max tokens"),
84
  gr.Slider(0.1, 4.0, 0.7, label="Temperature"),
85
  gr.Slider(0.1, 1.0, 0.95, label="Top-p"),
86
  ],
87
  outputs=gr.Textbox(label="Assistant reply"),
88
- title="StoryNest Chat (One‑Shot)",
89
  flagging_mode="never",
90
  )
91
 
92
- # ── 5) Launch with public API enabled ──────────────────────────────────────────
93
- demo.queue(api_open=True)
94
  if __name__ == "__main__":
95
  demo.launch()
 
6
  import requests
7
  from huggingface_hub import InferenceClient
8
 
9
+ # ── 0) Monkey-patch DNS for the HF Inference API hostname ────────────────────
10
  IP_POOL = [
11
  "3.165.206.104",
12
  "3.165.206.54",
 
27
  return _orig_getaddrinfo(host, port, family, socktype, proto, flags)
28
  socket.getaddrinfo = patched_getaddrinfo
29
 
30
+ # ── 1) Sanity-check DNS + HTTP for example.com ─────────────────────────────────
31
  try:
32
  ip = socket.gethostbyname("example.com")
33
  print("βœ… example.com β†’", ip)
 
43
  HF_TOKEN = os.environ["HF_TOKEN"]
44
  print("πŸ”‘ HF_TOKEN present?", bool(HF_TOKEN))
45
 
 
46
  client = InferenceClient(
47
+ model="HuggingFaceH4/zephyr-7b-beta",
48
+ token=HF_TOKEN
49
  )
50
 
51
+ # ── 3) One-shot chat function ─────────────────────────────────────────────────
52
  def respond(
53
  system_message: str,
54
  user_prompt: str,
 
67
  top_p=top_p,
68
  stream=False
69
  )
70
+ return resp.choices[0].message.content.strip()
71
 
72
+ # ── 4) Gradio Interface (no queue!) ────────────────────────────────────────────
 
 
 
 
73
  demo = gr.Interface(
74
  fn=respond,
75
  inputs=[
76
+ gr.Textbox(
77
+ "You are a helpful and friendly assistant for children. "
78
+ "All responses must be safe and positive.",
79
+ label="System message"
80
+ ),
81
  gr.Textbox(placeholder="Type your question…", label="User prompt"),
82
  gr.Slider(1, 2048, 512, label="Max tokens"),
83
  gr.Slider(0.1, 4.0, 0.7, label="Temperature"),
84
  gr.Slider(0.1, 1.0, 0.95, label="Top-p"),
85
  ],
86
  outputs=gr.Textbox(label="Assistant reply"),
87
+ title="StoryNest Chat (One-Shot)",
88
  flagging_mode="never",
89
  )
90
 
91
+ # ── 5) Launch normally ────────────────────────────────────────────────────────
 
92
  if __name__ == "__main__":
93
  demo.launch()