Threatthriver commited on
Commit
7476b7e
1 Parent(s): 79c5900

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -55
app.py CHANGED
@@ -1,22 +1,9 @@
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
3
- import logging
4
 
5
  # Initialize the InferenceClient with the model ID from Hugging Face
6
  client = InferenceClient(model="HuggingFaceH4/zephyr-7b-beta")
7
 
8
- # Set up logging
9
- logging.basicConfig(
10
- filename='chatbot_log.log',
11
- level=logging.INFO,
12
- format='%(asctime)s - %(levelname)s - %(message)s',
13
- )
14
-
15
- def log_conversation(user_message, bot_response):
16
- """Logs the conversation between the user and the AI."""
17
- logging.info(f"User: {user_message}")
18
- logging.info(f"Bot: {bot_response}")
19
-
20
  def respond(
21
  message: str,
22
  history: list[tuple[str, str]],
@@ -24,11 +11,22 @@ def respond(
24
  max_tokens: int,
25
  temperature: float,
26
  top_p: float,
27
- stop_sequence: str,
28
- stream_response: bool,
29
  ):
30
- """Generates a response from the AI model based on the user's message and chat history."""
 
 
 
 
 
 
 
 
 
31
 
 
 
 
 
32
  # Prepare the conversation history for the API call
33
  messages = [{"role": "system", "content": system_message}]
34
 
@@ -45,56 +43,41 @@ def respond(
45
  response = ""
46
 
47
  try:
48
- if stream_response:
49
- # Generate a response from the model with streaming
50
- for message in client.chat_completion(
51
- messages=messages,
52
- max_tokens=max_tokens,
53
- stream=True,
54
- temperature=temperature,
55
- top_p=top_p,
56
- stop=stop_sequence,
57
- ):
58
- token = message.choices[0].delta.get("content", "")
59
- response += token
60
- yield response
61
- else:
62
- # Generate a complete response without streaming
63
- result = client.chat_completion(
64
- messages=messages,
65
- max_tokens=max_tokens,
66
- stream=False,
67
- temperature=temperature,
68
- top_p=top_p,
69
- stop=stop_sequence,
70
- )
71
- response = result.choices[0].message.get("content", "")
72
- log_conversation(message, response)
73
  yield response
74
 
75
  except Exception as e:
76
- error_message = f"An error occurred: {str(e})"
77
- logging.error(error_message)
78
- yield error_message
79
 
80
  # Define the ChatInterface with additional input components for user customization
81
  demo = gr.ChatInterface(
82
  fn=respond,
83
  additional_inputs=[
84
- gr.Textbox(value="You are a friendly Chatbot.", label="System Message", lines=2),
85
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max New Tokens"),
86
- gr.Slider(minimum=0.1, maximum=2.0, value=0.7, step=0.1, label="Temperature"),
87
- gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (Nucleus Sampling)"),
88
- gr.Textbox(value="", label="Stop Sequence (optional)", lines=1),
89
- gr.Checkbox(label="Stream Response", value=True),
 
 
 
 
90
  ],
91
- title="AI Chatbot Interface",
92
- description="Interact with an AI chatbot powered by Hugging Face's Zephyr-7B model. Customize the chatbot's behavior and response generation settings.",
93
- theme="default",
94
  )
95
 
96
  # Launch the Gradio interface
97
  if __name__ == "__main__":
98
- logging.info("Launching the Gradio interface...")
99
  demo.launch()
100
- logging.info("Gradio interface launched successfully.")
 
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
  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.
17
+
18
+ Args:
19
+ message (str): The user's input message.
20
+ history (list): A list of tuples representing the conversation history (user, assistant).
21
+ system_message (str): A system-level message guiding the AI's behavior.
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.
28
+ """
29
+
30
  # Prepare the conversation history for the API call
31
  messages = [{"role": "system", "content": system_message}]
32
 
 
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.content
55
+ response += token
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
56
  yield response
57
 
58
  except Exception as e:
59
+ yield f"An error occurred: {str(e)}"
60
+
 
61
 
62
  # Define the ChatInterface with additional input components for user customization
63
  demo = gr.ChatInterface(
64
  fn=respond,
65
  additional_inputs=[
66
+ gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
67
+ gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
68
+ gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
69
+ gr.Slider(
70
+ minimum=0.1,
71
+ maximum=1.0,
72
+ value=0.95,
73
+ step=0.05,
74
+ label="Top-p (nucleus sampling)",
75
+ ),
76
  ],
77
+ title="Chatbot Interface",
78
+ description="A customizable chatbot interface using Hugging Face's Inference API.",
 
79
  )
80
 
81
  # Launch the Gradio interface
82
  if __name__ == "__main__":
 
83
  demo.launch()