aimanathar commited on
Commit
1604d9d
·
verified ·
1 Parent(s): 0c8d69e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +53 -40
app.py CHANGED
@@ -4,64 +4,77 @@ import os
4
 
5
  HF_TOKEN = os.getenv("HF_TOKEN")
6
 
7
- def respond(message, history: list[dict[str, str]], system_message, max_tokens, temperature, top_p):
8
  client = InferenceClient(token=HF_TOKEN, model="openai/gpt-oss-20b")
9
- messages = [{"role": "system", "content": system_message}]
 
10
  messages.extend(history)
11
  messages.append({"role": "user", "content": message})
12
 
13
  response = ""
14
  for msg in client.chat_completion(
15
  messages,
16
- max_tokens=max_tokens,
17
  stream=True,
18
- temperature=temperature,
19
- top_p=top_p,
20
  ):
21
  if msg.choices and msg.choices[0].delta.content:
22
  response += msg.choices[0].delta.content
23
  yield response
24
 
 
 
25
  custom_css = """
26
- .gradio-container {
27
- background-color: #f9f9fb;
28
- font-family: Arial, sans-serif;
29
- }
30
- .gradio-chatbox .message.user {
31
- background-color: #daf7dc;
32
- border-radius: 12px;
33
- padding: 8px;
34
- margin: 4px 0;
 
 
35
  }
36
- .gradio-chatbox .message.bot {
37
- background-color: #cce5ff;
38
- border-radius: 12px;
39
- padding: 8px;
40
- margin: 4px 0;
41
- }
42
- input[type="text"] {
43
- border-radius: 8px;
44
- border: 1px solid #ccc;
45
- }
46
- button {
47
- background-color: #007bff;
48
  color: white;
49
- border-radius: 8px;
 
 
 
 
 
 
 
 
 
50
  }
51
  """
52
 
53
- with gr.Blocks(theme=gr.themes.Soft(), css=custom_css) as demo:
54
- chatbot = gr.ChatInterface(
55
- respond,
56
- type="messages",
57
- additional_inputs=[
58
- gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
59
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
60
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
61
- gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p"),
62
- ]
63
- )
64
- chatbot.render()
 
 
 
 
 
 
65
 
66
  if __name__ == "__main__":
67
- demo.launch(share=True)
 
4
 
5
  HF_TOKEN = os.getenv("HF_TOKEN")
6
 
7
+ def respond(message, history: list[dict[str, str]]):
8
  client = InferenceClient(token=HF_TOKEN, model="openai/gpt-oss-20b")
9
+
10
+ messages = [{"role": "system", "content": "You are a friendly chatbot."}]
11
  messages.extend(history)
12
  messages.append({"role": "user", "content": message})
13
 
14
  response = ""
15
  for msg in client.chat_completion(
16
  messages,
17
+ max_tokens=512,
18
  stream=True,
19
+ temperature=0.7,
20
+ top_p=0.95
21
  ):
22
  if msg.choices and msg.choices[0].delta.content:
23
  response += msg.choices[0].delta.content
24
  yield response
25
 
26
+
27
+ # CSS for floating button + hidden chatbot
28
  custom_css = """
29
+ #chatbot-container {
30
+ display: none;
31
+ position: fixed;
32
+ bottom: 80px;
33
+ right: 30px;
34
+ width: 350px;
35
+ max-height: 500px;
36
+ z-index: 9999;
37
+ border-radius: 15px;
38
+ overflow: hidden;
39
+ box-shadow: 0px 5px 20px rgba(0,0,0,0.3);
40
  }
41
+ #chatbot-toggle {
42
+ position: fixed;
43
+ bottom: 20px;
44
+ right: 30px;
45
+ background: #7E498B;
 
 
 
 
 
 
 
46
  color: white;
47
+ border-radius: 50%;
48
+ width: 60px;
49
+ height: 60px;
50
+ font-size: 28px;
51
+ display: flex;
52
+ align-items: center;
53
+ justify-content: center;
54
+ cursor: pointer;
55
+ box-shadow: 0px 5px 10px rgba(0,0,0,0.2);
56
+ z-index: 10000;
57
  }
58
  """
59
 
60
+ with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as demo:
61
+ with gr.Box(elem_id="chatbot-container"):
62
+ gr.ChatInterface(respond, type="messages")
63
+
64
+ # Floating purple chat icon
65
+ gr.HTML("""
66
+ <div id="chatbot-toggle" onclick="toggleChat()">💬</div>
67
+ <script>
68
+ function toggleChat() {
69
+ var chat = document.getElementById("chatbot-container");
70
+ if (chat.style.display === "none" || chat.style.display === "") {
71
+ chat.style.display = "block";
72
+ } else {
73
+ chat.style.display = "none";
74
+ }
75
+ }
76
+ </script>
77
+ """)
78
 
79
  if __name__ == "__main__":
80
+ demo.launch()