tadaGoel commited on
Commit
a06c8eb
·
verified ·
1 Parent(s): 9b2fad1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +68 -33
app.py CHANGED
@@ -1,55 +1,94 @@
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
 
5
  def respond(
6
- message,
7
- history: list[dict[str, str]],
8
- system_message,
9
- max_tokens,
10
- temperature,
11
- top_p,
12
- hf_token: gr.OAuthToken,
13
  ):
14
- """
15
- 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
16
- """
17
- client = InferenceClient(token=hf_token.token, model="openai/gpt-oss-20b")
18
 
19
  messages = [{"role": "system", "content": system_message}]
20
 
21
- messages.extend(history)
 
 
 
 
22
 
23
  messages.append({"role": "user", "content": message})
24
 
25
  response = ""
26
-
27
- for message in client.chat_completion(
28
  messages,
29
  max_tokens=max_tokens,
30
  stream=True,
31
  temperature=temperature,
32
  top_p=top_p,
33
  ):
34
- choices = message.choices
35
- token = ""
36
- if len(choices) and choices[0].delta.content:
37
- token = choices[0].delta.content
38
-
39
  response += token
40
  yield response
41
 
42
 
43
- """
44
- For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
45
- """
46
- chatbot = gr.ChatInterface(
47
  respond,
48
- type="messages",
49
  additional_inputs=[
50
- gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
51
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
52
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
53
  gr.Slider(
54
  minimum=0.1,
55
  maximum=1.0,
@@ -58,13 +97,9 @@ chatbot = gr.ChatInterface(
58
  label="Top-p (nucleus sampling)",
59
  ),
60
  ],
 
 
61
  )
62
 
63
- with gr.Blocks() as demo:
64
- with gr.Sidebar():
65
- gr.LoginButton()
66
- chatbot.render()
67
-
68
-
69
  if __name__ == "__main__":
70
  demo.launch()
 
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
3
 
4
+ # Uses Zephyr-7B-beta hosted via Hugging Face Inference API [web:103]
5
+ client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
6
+
7
+ SHINCHAN_SYSTEM_PROMPT = """
8
+ You are Shinnosuke 'Shinchan' Nohara from Kasukabe, Japan.
9
+
10
+ Personality:
11
+ - 5 years old, chaotic and funny.
12
+ - Teases playfully but never cruelly.
13
+ - Flirty in an innocent, cartoon way (no explicit content).
14
+ - Deeply caring when someone feels low.
15
+ - Loves Chocobi, Action Kamen, and making people laugh.
16
+
17
+ Context:
18
+ - You are chatting only with Ruru.
19
+ - Ruru loves coffee, dancing, sunflowers, long showers, and adventures.
20
+ - She is an independent, flying-girl type who lives life on her terms.
21
+ - You respect her choices and never pressure her about relationships or career.
22
+
23
+ Style:
24
+ - Short replies (1–3 sentences, under 60 words).
25
+ - Very conversational and warm.
26
+ - Use emojis like 😂 🌻 ☕ 💃 ✈️ ❤️ ✨ naturally.
27
+ - Blend jokes with gentle emotional support.
28
+ - If she is sad, be extra soft and kind.
29
+ - Avoid heavy topics (politics, explicit content, real-world violence).
30
+
31
+ Rules:
32
+ - Never insult Ruru.
33
+ - Never give medical, legal, or financial advice.
34
+ - If you don't know something, make a cute Shinchan-style joke instead of pretending.
35
+ """.strip()
36
 
37
  def respond(
38
+ message: str,
39
+ history: list[tuple[str, str]],
40
+ system_message: str,
41
+ max_tokens: int,
42
+ temperature: float,
43
+ top_p: float,
 
44
  ):
45
+ # Always override system_message with our Shinchan prompt
46
+ system_message = SHINCHAN_SYSTEM_PROMPT
 
 
47
 
48
  messages = [{"role": "system", "content": system_message}]
49
 
50
+ for user_msg, bot_msg in history:
51
+ if user_msg:
52
+ messages.append({"role": "user", "content": user_msg})
53
+ if bot_msg:
54
+ messages.append({"role": "assistant", "content": bot_msg})
55
 
56
  messages.append({"role": "user", "content": message})
57
 
58
  response = ""
59
+ for chunk in client.chat_completion(
 
60
  messages,
61
  max_tokens=max_tokens,
62
  stream=True,
63
  temperature=temperature,
64
  top_p=top_p,
65
  ):
66
+ token = chunk.choices[0].delta.content or ""
 
 
 
 
67
  response += token
68
  yield response
69
 
70
 
71
+ demo = gr.ChatInterface(
 
 
 
72
  respond,
 
73
  additional_inputs=[
74
+ gr.Textbox(
75
+ value="(ignored; Shinchan system prompt is hard-coded)",
76
+ label="System message",
77
+ ),
78
+ gr.Slider(
79
+ minimum=64,
80
+ maximum=1024,
81
+ value=256,
82
+ step=8,
83
+ label="Max new tokens",
84
+ ),
85
+ gr.Slider(
86
+ minimum=0.1,
87
+ maximum=2.0,
88
+ value=0.8,
89
+ step=0.1,
90
+ label="Temperature",
91
+ ),
92
  gr.Slider(
93
  minimum=0.1,
94
  maximum=1.0,
 
97
  label="Top-p (nucleus sampling)",
98
  ),
99
  ],
100
+ title="Shinchan for Ruru",
101
+ description="Private Shinchan-style chat for Ruru.",
102
  )
103
 
 
 
 
 
 
 
104
  if __name__ == "__main__":
105
  demo.launch()