Threatthriver commited on
Commit
6c40506
1 Parent(s): 2178804

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +58 -11
app.py CHANGED
@@ -1,9 +1,29 @@
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
 
 
3
 
4
  # Initialize the InferenceClient with the model ID from Hugging Face
5
  client = InferenceClient(model="HuggingFaceH4/zephyr-7b-beta")
6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
  def respond(
8
  message: str,
9
  history: list[tuple[str, str]],
@@ -11,6 +31,8 @@ def respond(
11
  max_tokens: int,
12
  temperature: float,
13
  top_p: float,
 
 
14
  ):
15
  """
16
  Generates a response from the AI model based on the user's message and chat history.
@@ -22,6 +44,8 @@ def respond(
22
  max_tokens (int): The maximum number of tokens for the output.
23
  temperature (float): Sampling temperature for controlling the randomness.
24
  top_p (float): Top-p (nucleus sampling) for controlling diversity.
 
 
25
 
26
  Yields:
27
  str: The AI's response as it is generated.
@@ -43,20 +67,37 @@ def respond(
43
  response = ""
44
 
45
  try:
46
- # Generate a response from the model with streaming
47
- for message in client.chat_completion(
48
- messages=messages,
49
- max_tokens=max_tokens,
50
- stream=True,
51
- temperature=temperature,
52
- top_p=top_p,
53
- ):
54
- token = message.choices[0].delta.get("content", "")
55
- response += token
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
56
  yield response
57
 
58
  except Exception as e:
59
- yield f"An error occurred: {str(e)}"
 
 
60
 
61
  # Define the ChatInterface with additional input components for user customization
62
  demo = gr.ChatInterface(
@@ -66,11 +107,17 @@ demo = gr.ChatInterface(
66
  gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max New Tokens"),
67
  gr.Slider(minimum=0.1, maximum=2.0, value=0.7, step=0.1, label="Temperature"),
68
  gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (Nucleus Sampling)"),
 
 
69
  ],
70
  title="AI Chatbot Interface",
71
  description="Interact with an AI chatbot powered by Hugging Face's Zephyr-7B model. Customize the chatbot's behavior and response generation settings.",
 
 
72
  )
73
 
74
  # Launch the Gradio interface
75
  if __name__ == "__main__":
 
76
  demo.launch()
 
 
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
3
+ import logging
4
+ from datetime import datetime
5
 
6
  # Initialize the InferenceClient with the model ID from Hugging Face
7
  client = InferenceClient(model="HuggingFaceH4/zephyr-7b-beta")
8
 
9
+ # Set up logging
10
+ logging.basicConfig(
11
+ filename='chatbot_log.log',
12
+ level=logging.INFO,
13
+ format='%(asctime)s - %(levelname)s - %(message)s',
14
+ )
15
+
16
+ def log_conversation(user_message, bot_response):
17
+ """
18
+ Logs the conversation between the user and the AI.
19
+
20
+ Args:
21
+ user_message (str): The user's input message.
22
+ bot_response (str): The AI's response.
23
+ """
24
+ logging.info(f"User: {user_message}")
25
+ logging.info(f"Bot: {bot_response}")
26
+
27
  def respond(
28
  message: str,
29
  history: list[tuple[str, str]],
 
31
  max_tokens: int,
32
  temperature: float,
33
  top_p: float,
34
+ stop_sequence: str,
35
+ stream_response: bool,
36
  ):
37
  """
38
  Generates a response from the AI model based on the user's message and chat history.
 
44
  max_tokens (int): The maximum number of tokens for the output.
45
  temperature (float): Sampling temperature for controlling the randomness.
46
  top_p (float): Top-p (nucleus sampling) for controlling diversity.
47
+ stop_sequence (str): A custom stop sequence to end the response generation.
48
+ stream_response (bool): Whether to stream the response or return it as a whole.
49
 
50
  Yields:
51
  str: The AI's response as it is generated.
 
67
  response = ""
68
 
69
  try:
70
+ if stream_response:
71
+ # Generate a response from the model with streaming
72
+ for message in client.chat_completion(
73
+ messages=messages,
74
+ max_tokens=max_tokens,
75
+ stream=True,
76
+ temperature=temperature,
77
+ top_p=top_p,
78
+ stop=stop_sequence,
79
+ ):
80
+ token = message.choices[0].delta.get("content", "")
81
+ response += token
82
+ yield response
83
+ else:
84
+ # Generate a complete response without streaming
85
+ result = client.chat_completion(
86
+ messages=messages,
87
+ max_tokens=max_tokens,
88
+ stream=False,
89
+ temperature=temperature,
90
+ top_p=top_p,
91
+ stop=stop_sequence,
92
+ )
93
+ response = result.choices[0].message.get("content", "")
94
+ log_conversation(message, response)
95
  yield response
96
 
97
  except Exception as e:
98
+ error_message = f"An error occurred: {str(e)}"
99
+ logging.error(error_message)
100
+ yield error_message
101
 
102
  # Define the ChatInterface with additional input components for user customization
103
  demo = gr.ChatInterface(
 
107
  gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max New Tokens"),
108
  gr.Slider(minimum=0.1, maximum=2.0, value=0.7, step=0.1, label="Temperature"),
109
  gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (Nucleus Sampling)"),
110
+ gr.Textbox(value="", label="Stop Sequence (optional)", lines=1),
111
+ gr.Checkbox(label="Stream Response", value=True),
112
  ],
113
  title="AI Chatbot Interface",
114
  description="Interact with an AI chatbot powered by Hugging Face's Zephyr-7B model. Customize the chatbot's behavior and response generation settings.",
115
+ theme="default",
116
+ allow_flagging="never",
117
  )
118
 
119
  # Launch the Gradio interface
120
  if __name__ == "__main__":
121
+ logging.info("Launching the Gradio interface...")
122
  demo.launch()
123
+ logging.info("Gradio interface launched successfully.")