Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
@@ -4,42 +4,24 @@ from huggingface_hub import InferenceClient
|
|
4 |
# Initialize the InferenceClient with the model ID from Hugging Face
|
5 |
client = InferenceClient(model="HuggingFaceH4/zephyr-7b-beta")
|
6 |
|
7 |
-
def
|
8 |
-
|
9 |
-
history: list[tuple[str, str]],
|
10 |
-
system_message: 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
|
17 |
-
|
18 |
Args:
|
19 |
-
|
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
|
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 |
-
|
33 |
-
for user_input, assistant_response in history:
|
34 |
-
if user_input:
|
35 |
-
messages.append({"role": "user", "content": user_input})
|
36 |
-
if assistant_response:
|
37 |
-
messages.append({"role": "assistant", "content": assistant_response})
|
38 |
-
|
39 |
-
# Add the latest user message to the conversation
|
40 |
-
messages.append({"role": "user", "content": message})
|
41 |
-
|
42 |
-
# Initialize an empty response
|
43 |
response = ""
|
44 |
|
45 |
try:
|
@@ -58,6 +40,56 @@ def respond(
|
|
58 |
except Exception as e:
|
59 |
yield f"An error occurred: {str(e)}"
|
60 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
61 |
# Define the UI layout with improved structure
|
62 |
with gr.Blocks() as demo:
|
63 |
gr.Markdown("# 🧠 AI Chatbot Interface")
|
@@ -96,19 +128,21 @@ with gr.Blocks() as demo:
|
|
96 |
clear_btn = gr.Button("Clear Chat")
|
97 |
|
98 |
# Handle sample prompt selection
|
99 |
-
def update_message(prompt):
|
100 |
return prompt
|
101 |
|
102 |
sample_prompt.change(fn=update_message, inputs=sample_prompt, outputs=message)
|
103 |
|
104 |
# Update the chatbot with the new message and response
|
105 |
-
submit_btn.click(
|
106 |
-
|
107 |
-
|
108 |
-
|
|
|
|
|
109 |
|
110 |
# Clear the chat history
|
111 |
-
def clear_chat():
|
112 |
return []
|
113 |
|
114 |
clear_btn.click(fn=clear_chat, inputs=None, outputs=chatbot)
|
|
|
4 |
# Initialize the InferenceClient with the model ID from Hugging Face
|
5 |
client = InferenceClient(model="HuggingFaceH4/zephyr-7b-beta")
|
6 |
|
7 |
+
def generate_response(
|
8 |
+
messages: list[dict],
|
|
|
|
|
9 |
max_tokens: int,
|
10 |
temperature: float,
|
11 |
top_p: float,
|
12 |
):
|
13 |
"""
|
14 |
+
Generates a response from the AI model based on the provided messages.
|
15 |
+
|
16 |
Args:
|
17 |
+
messages (list): A list of messages representing the conversation history.
|
|
|
|
|
18 |
max_tokens (int): The maximum number of tokens for the output.
|
19 |
+
temperature (float): Sampling temperature for controlling randomness.
|
20 |
top_p (float): Top-p (nucleus sampling) for controlling diversity.
|
21 |
|
22 |
Yields:
|
23 |
str: The AI's response as it is generated.
|
24 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
response = ""
|
26 |
|
27 |
try:
|
|
|
40 |
except Exception as e:
|
41 |
yield f"An error occurred: {str(e)}"
|
42 |
|
43 |
+
def build_messages(system_message: str, history: list[tuple[str, str]], user_message: str) -> list[dict]:
|
44 |
+
"""
|
45 |
+
Builds the list of messages for the model based on system message, history, and latest user message.
|
46 |
+
|
47 |
+
Args:
|
48 |
+
system_message (str): A system-level message guiding the AI's behavior.
|
49 |
+
history (list): A list of tuples representing the conversation history (user, assistant).
|
50 |
+
user_message (str): The latest user message.
|
51 |
+
|
52 |
+
Returns:
|
53 |
+
list: A list of message dictionaries formatted for the API call.
|
54 |
+
"""
|
55 |
+
messages = [{"role": "system", "content": system_message}]
|
56 |
+
|
57 |
+
for user_input, assistant_response in history:
|
58 |
+
if user_input:
|
59 |
+
messages.append({"role": "user", "content": user_input})
|
60 |
+
if assistant_response:
|
61 |
+
messages.append({"role": "assistant", "content": assistant_response})
|
62 |
+
|
63 |
+
# Add the latest user message to the conversation
|
64 |
+
messages.append({"role": "user", "content": user_message})
|
65 |
+
|
66 |
+
return messages
|
67 |
+
|
68 |
+
def respond(
|
69 |
+
message: str,
|
70 |
+
history: list[tuple[str, str]],
|
71 |
+
system_message: str,
|
72 |
+
max_tokens: int,
|
73 |
+
temperature: float,
|
74 |
+
top_p: float,
|
75 |
+
):
|
76 |
+
"""
|
77 |
+
Handles the interaction with the model to generate a response based on user input and chat history.
|
78 |
+
|
79 |
+
Args:
|
80 |
+
message (str): The user's input message.
|
81 |
+
history (list): A list of tuples representing the conversation history (user, assistant).
|
82 |
+
system_message (str): A system-level message guiding the AI's behavior.
|
83 |
+
max_tokens (int): The maximum number of tokens for the output.
|
84 |
+
temperature (float): Sampling temperature for controlling randomness.
|
85 |
+
top_p (float): Top-p (nucleus sampling) for controlling diversity.
|
86 |
+
|
87 |
+
Yields:
|
88 |
+
str: The AI's response as it is generated.
|
89 |
+
"""
|
90 |
+
messages = build_messages(system_message, history, message)
|
91 |
+
yield from generate_response(messages, max_tokens, temperature, top_p)
|
92 |
+
|
93 |
# Define the UI layout with improved structure
|
94 |
with gr.Blocks() as demo:
|
95 |
gr.Markdown("# 🧠 AI Chatbot Interface")
|
|
|
128 |
clear_btn = gr.Button("Clear Chat")
|
129 |
|
130 |
# Handle sample prompt selection
|
131 |
+
def update_message(prompt: str) -> str:
|
132 |
return prompt
|
133 |
|
134 |
sample_prompt.change(fn=update_message, inputs=sample_prompt, outputs=message)
|
135 |
|
136 |
# Update the chatbot with the new message and response
|
137 |
+
submit_btn.click(
|
138 |
+
fn=respond,
|
139 |
+
inputs=[message, chatbot, system_message, max_tokens, temperature, top_p],
|
140 |
+
outputs=[chatbot],
|
141 |
+
show_progress=True
|
142 |
+
)
|
143 |
|
144 |
# Clear the chat history
|
145 |
+
def clear_chat() -> list:
|
146 |
return []
|
147 |
|
148 |
clear_btn.click(fn=clear_chat, inputs=None, outputs=chatbot)
|