Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -35,7 +35,7 @@ PRE_PROMPT = load_decrypted_preprompt()
|
|
| 35 |
|
| 36 |
# Default parameters for the QA chain
|
| 37 |
DEFAULT_TEMPERATURE = 0.7
|
| 38 |
-
DEFAULT_MAX_TOKENS =
|
| 39 |
DEFAULT_TOP_K = 3
|
| 40 |
DEFAULT_TOP_P = 0.95
|
| 41 |
|
|
@@ -116,19 +116,49 @@ def update_chat(message, history):
|
|
| 116 |
history.append({"role": "user", "content": message})
|
| 117 |
return history, message, ""
|
| 118 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 119 |
def get_assistant_response(message, history, max_tokens, temperature, top_p, qa_chain_state_dict):
|
| 120 |
-
"""
|
| 121 |
-
Generate the assistant's response using the QA chain (if available) or fallback to plain chat.
|
| 122 |
-
The pre-prompt is always included by concatenating it to the user's new question.
|
| 123 |
-
"""
|
| 124 |
qa_chain = qa_chain_state_dict.get("qa_chain")
|
| 125 |
|
| 126 |
if qa_chain is not None:
|
| 127 |
-
# Format history to the plain-text format expected by the QA chain
|
| 128 |
formatted_history = format_chat_history(history)
|
| 129 |
-
# Prepend the pre-prompt to the current question
|
| 130 |
combined_question = PRE_PROMPT + "\n" + message
|
|
|
|
| 131 |
response = qa_chain.invoke({"question": combined_question, "chat_history": formatted_history})
|
|
|
|
| 132 |
answer = response.get("answer", "")
|
| 133 |
history.append({"role": "assistant", "content": answer})
|
| 134 |
return history, {"qa_chain": qa_chain}
|
|
@@ -143,6 +173,7 @@ def get_assistant_response(message, history, max_tokens, temperature, top_p, qa_
|
|
| 143 |
temperature=temperature,
|
| 144 |
top_p=top_p,
|
| 145 |
)
|
|
|
|
| 146 |
for token_message in result:
|
| 147 |
token = token_message.choices[0].delta.content
|
| 148 |
response += token
|
|
@@ -190,10 +221,19 @@ with gr.Blocks(theme=gr.themes.Default(primary_hue="sky")) as demo:
|
|
| 190 |
document.documentElement.setAttribute('data-theme', 'light');
|
| 191 |
</script>
|
| 192 |
|
|
|
|
|
|
|
| 193 |
<style>
|
| 194 |
:root {
|
| 195 |
color-scheme: light !important;
|
|
|
|
|
|
|
| 196 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 197 |
.example-row {
|
| 198 |
flex-grow: 1 !important;
|
| 199 |
width: 100% !important;
|
|
|
|
| 35 |
|
| 36 |
# Default parameters for the QA chain
|
| 37 |
DEFAULT_TEMPERATURE = 0.7
|
| 38 |
+
DEFAULT_MAX_TOKENS = 1024
|
| 39 |
DEFAULT_TOP_K = 3
|
| 40 |
DEFAULT_TOP_P = 0.95
|
| 41 |
|
|
|
|
| 116 |
history.append({"role": "user", "content": message})
|
| 117 |
return history, message, ""
|
| 118 |
|
| 119 |
+
# def get_assistant_response(message, history, max_tokens, temperature, top_p, qa_chain_state_dict):
|
| 120 |
+
# """
|
| 121 |
+
# Generate the assistant's response using the QA chain (if available) or fallback to plain chat.
|
| 122 |
+
# The pre-prompt is always included by concatenating it to the user's new question.
|
| 123 |
+
# """
|
| 124 |
+
# qa_chain = qa_chain_state_dict.get("qa_chain")
|
| 125 |
+
|
| 126 |
+
# if qa_chain is not None:
|
| 127 |
+
# # Format history to the plain-text format expected by the QA chain
|
| 128 |
+
# formatted_history = format_chat_history(history)
|
| 129 |
+
# # Prepend the pre-prompt to the current question
|
| 130 |
+
# combined_question = PRE_PROMPT + "\n" + message
|
| 131 |
+
# response = qa_chain.invoke({"question": combined_question, "chat_history": formatted_history})
|
| 132 |
+
# answer = response.get("answer", "")
|
| 133 |
+
# history.append({"role": "assistant", "content": answer})
|
| 134 |
+
# return history, {"qa_chain": qa_chain}
|
| 135 |
+
|
| 136 |
+
# # Fallback: Plain Chat Mode using the InferenceClient (pre-prompt already included here)
|
| 137 |
+
# messages = [{"role": "system", "content": PRE_PROMPT}] + history
|
| 138 |
+
# response = ""
|
| 139 |
+
# result = client.chat_completion(
|
| 140 |
+
# messages,
|
| 141 |
+
# max_tokens=max_tokens,
|
| 142 |
+
# stream=False,
|
| 143 |
+
# temperature=temperature,
|
| 144 |
+
# top_p=top_p,
|
| 145 |
+
# )
|
| 146 |
+
# for token_message in result:
|
| 147 |
+
# token = token_message.choices[0].delta.content
|
| 148 |
+
# response += token
|
| 149 |
+
|
| 150 |
+
# history.append({"role": "assistant", "content": response})
|
| 151 |
+
# return history, {"qa_chain": qa_chain}
|
| 152 |
+
|
| 153 |
def get_assistant_response(message, history, max_tokens, temperature, top_p, qa_chain_state_dict):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 154 |
qa_chain = qa_chain_state_dict.get("qa_chain")
|
| 155 |
|
| 156 |
if qa_chain is not None:
|
|
|
|
| 157 |
formatted_history = format_chat_history(history)
|
|
|
|
| 158 |
combined_question = PRE_PROMPT + "\n" + message
|
| 159 |
+
print("Combined Question:", combined_question) # Debug print
|
| 160 |
response = qa_chain.invoke({"question": combined_question, "chat_history": formatted_history})
|
| 161 |
+
print("QA Chain Response:", response) # Debug print
|
| 162 |
answer = response.get("answer", "")
|
| 163 |
history.append({"role": "assistant", "content": answer})
|
| 164 |
return history, {"qa_chain": qa_chain}
|
|
|
|
| 173 |
temperature=temperature,
|
| 174 |
top_p=top_p,
|
| 175 |
)
|
| 176 |
+
print("Chat Completion Result:", result) # Debug print
|
| 177 |
for token_message in result:
|
| 178 |
token = token_message.choices[0].delta.content
|
| 179 |
response += token
|
|
|
|
| 221 |
document.documentElement.setAttribute('data-theme', 'light');
|
| 222 |
</script>
|
| 223 |
|
| 224 |
+
<style>
|
| 225 |
+
|
| 226 |
<style>
|
| 227 |
:root {
|
| 228 |
color-scheme: light !important;
|
| 229 |
+
background-color: #fff !important;
|
| 230 |
+
color: #333 !important;
|
| 231 |
}
|
| 232 |
+
body, .gradio-container, .chatbot, .hf-chat-input {
|
| 233 |
+
background-color: #fff !important;
|
| 234 |
+
color: #333 !important;
|
| 235 |
+
}
|
| 236 |
+
|
| 237 |
.example-row {
|
| 238 |
flex-grow: 1 !important;
|
| 239 |
width: 100% !important;
|