AnkitkumarMalde commited on
Commit
55c872e
·
verified ·
1 Parent(s): f8c6721

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -57
app.py CHANGED
@@ -1,85 +1,44 @@
1
- import os
2
- # This automatically picks up the secret you added in Settings
3
- token = os.getenv("admin_token")
4
-
5
  import gradio as gr
6
  from huggingface_hub import InferenceClient
7
- import os
8
-
9
- # 1. Initialize Client (Uses HF servers, saves your Space's memory)
10
- # This will automatically use the HF_TOKEN secret you added to Settings
11
- client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
12
-
13
- def respond(message, history):
14
- messages = []
15
-
16
- # Build history for the API
17
- for user_msg, bot_msg in history:
18
- messages.append({"role": "user", "content": user_msg})
19
- messages.append({"role": "assistant", "content": bot_msg})
20
-
21
- messages.append({"role": "user", "content": message})
22
-
23
- response = ""
24
 
25
- # Stream the response back
26
- for message in client.chat_completion(
27
- messages,
28
- max_tokens=512,
29
- stream=True,
30
- temperature=0.7,
31
- top_p=0.95,
32
- ):
33
- token = message.choices[0].delta.content
34
- response += token
35
- yield response
36
 
37
- # 2. Simplified UI
38
  def chat_function(message, history):
39
- """
40
- This function takes the user input and the chat history,
41
- sends it to the model, and yields the response.
42
- """
43
- messages = [{"role": "system", "content": "You are a helpful and professional AI assistant."}]
44
 
45
- # Add previous conversation turns to the memory
46
  for user_msg, bot_msg in history:
47
  messages.append({"role": "user", "content": user_msg})
48
  messages.append({"role": "assistant", "content": bot_msg})
49
 
50
- # Add the current user message
51
  messages.append({"role": "user", "content": message})
52
 
53
  response = ""
54
 
55
- # Call the model and stream the result
56
  for message_obj in client.chat_completion(
57
- messages,
58
  max_tokens=512,
59
  stream=True,
60
  temperature=0.7,
61
  top_p=0.95,
62
  ):
63
- token = message_obj.choices[0].delta.content
64
- if token:
65
- response += token
66
- yield response
67
 
68
- # 3. BUILD THE UI (Linking to the function defined above)
69
  demo = gr.ChatInterface(
70
- fn=chat_function, # This name MUST match the function name above
71
  title="Premium AI Chatbot",
72
  description="A high-performance assistant hosted on Hugging Face.",
73
- theme="soft", # Gives it a modern, rounded look
74
- examples=["What are the benefits of RPA?", "How to optimize SIP investments?", "Explain financial planning."],
 
 
 
 
75
  cache_examples=False,
76
- chatbot=gr.Chatbot(
77
- show_label=False,
78
- show_share_button=False,
79
- show_copy_button=True,
80
- likeable=True,
81
- layout="bubble"
82
- )
83
  )
84
 
85
  if __name__ == "__main__":
 
 
 
 
 
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
 
4
+ # Initialize client
5
+ client = InferenceClient(model="HuggingFaceH4/zephyr-7b-beta")
 
 
 
 
 
 
 
 
 
6
 
 
7
  def chat_function(message, history):
8
+ messages = [
9
+ {"role": "system", "content": "You are a helpful and professional AI assistant."}
10
+ ]
 
 
11
 
 
12
  for user_msg, bot_msg in history:
13
  messages.append({"role": "user", "content": user_msg})
14
  messages.append({"role": "assistant", "content": bot_msg})
15
 
 
16
  messages.append({"role": "user", "content": message})
17
 
18
  response = ""
19
 
 
20
  for message_obj in client.chat_completion(
21
+ messages=messages,
22
  max_tokens=512,
23
  stream=True,
24
  temperature=0.7,
25
  top_p=0.95,
26
  ):
27
+ token = message_obj.choices[0].delta.content or ""
28
+ response += token
29
+ yield response
 
30
 
 
31
  demo = gr.ChatInterface(
32
+ fn=chat_function,
33
  title="Premium AI Chatbot",
34
  description="A high-performance assistant hosted on Hugging Face.",
35
+ theme="soft",
36
+ examples=[
37
+ "What are the benefits of RPA?",
38
+ "How to optimize SIP investments?",
39
+ "Explain financial planning."
40
+ ],
41
  cache_examples=False,
 
 
 
 
 
 
 
42
  )
43
 
44
  if __name__ == "__main__":