Update app.py
Browse files
app.py
CHANGED
|
@@ -186,21 +186,33 @@ def get_default_system_prompt(personalized_data):
|
|
| 186 |
"""
|
| 187 |
print(DEFAULT_SYSTEM_PROMPT)
|
| 188 |
return DEFAULT_SYSTEM_PROMPT
|
| 189 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 190 |
@spaces.GPU
|
| 191 |
def generate(
|
| 192 |
-
request: gr.Request, # To fetch query params
|
| 193 |
message: str,
|
| 194 |
chat_history: list[tuple[str, str]],
|
| 195 |
-
input_prompt: str,
|
| 196 |
max_new_tokens: int = 1024,
|
| 197 |
temperature: float = 0.6,
|
| 198 |
top_p: float = 0.9,
|
| 199 |
top_k: int = 50,
|
| 200 |
repetition_penalty: float = 1.2,
|
| 201 |
-
|
| 202 |
-
|
| 203 |
|
|
|
|
|
|
|
| 204 |
# Fetch query params
|
| 205 |
params = request.query_params
|
| 206 |
print('those are the query params')
|
|
@@ -214,30 +226,21 @@ def generate(
|
|
| 214 |
if session_index is not None:
|
| 215 |
print("Session index:", session_index)
|
| 216 |
else:
|
| 217 |
-
print("Session index
|
| 218 |
|
|
|
|
| 219 |
personalized_data = fetch_personalized_data(session_index)
|
| 220 |
-
DEFAULT_SYSTEM_PROMPT = get_default_system_prompt(personalized_data)
|
| 221 |
-
|
| 222 |
-
## trust-game-llama-2-7b-chat
|
| 223 |
-
# app.py
|
| 224 |
-
def construct_input_prompt(chat_history, message):
|
| 225 |
-
input_prompt = f"<s>[INST] <<SYS>>\n{DEFAULT_SYSTEM_PROMPT}\n<</SYS>>\n\n "
|
| 226 |
-
for user, assistant in chat_history:
|
| 227 |
-
input_prompt += f"{user} [/INST] {assistant} <s>[INST] "
|
| 228 |
-
input_prompt += f"{message} [/INST] "
|
| 229 |
-
return input_prompt
|
| 230 |
-
|
| 231 |
-
# Construct the input prompt using the functions from the construct_input_prompt function
|
| 232 |
-
input_prompt = construct_input_prompt(chat_history, message)
|
| 233 |
|
|
|
|
|
|
|
|
|
|
| 234 |
# Move the condition here after the assignment
|
| 235 |
if input_prompt:
|
| 236 |
conversation.append({"role": "system", "content": input_prompt})
|
| 237 |
|
| 238 |
# Convert input prompt to tensor
|
| 239 |
input_ids = tokenizer(input_prompt, return_tensors="pt").to(model.device)
|
| 240 |
-
|
| 241 |
for user, assistant in chat_history:
|
| 242 |
conversation.extend([{"role": "user", "content": user}, {"role": "assistant", "content": assistant}])
|
| 243 |
conversation.append({"role": "user", "content": message})
|
|
@@ -249,10 +252,8 @@ def generate(
|
|
| 249 |
input_ids = input_ids.to(model.device)
|
| 250 |
|
| 251 |
# Set up the TextIteratorStreamer
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
streamer = TextIteratorStreamer(tokenizer, timeout=30.0, skip_prompt=True, skip_special_tokens=True)
|
| 255 |
-
|
| 256 |
# Set up the generation arguments
|
| 257 |
generate_kwargs = dict(
|
| 258 |
{"input_ids": input_ids},
|
|
@@ -275,7 +276,6 @@ def generate(
|
|
| 275 |
for text in streamer:
|
| 276 |
outputs.append(text)
|
| 277 |
yield "".join(outputs)
|
| 278 |
-
|
| 279 |
|
| 280 |
chat_interface = gr.ChatInterface(
|
| 281 |
fn=generate,
|
|
|
|
| 186 |
"""
|
| 187 |
print(DEFAULT_SYSTEM_PROMPT)
|
| 188 |
return DEFAULT_SYSTEM_PROMPT
|
| 189 |
+
|
| 190 |
+
## trust-game-llama-2-7b-chat
|
| 191 |
+
# app.py
|
| 192 |
+
def construct_input_prompt(chat_history, message, personalized_data):
|
| 193 |
+
input_prompt = f"<s>[INST] <<SYS>>\n{get_default_system_prompt(personalized_data)}\n<</SYS>>\n\n "
|
| 194 |
+
for user, assistant in chat_history:
|
| 195 |
+
input_prompt += f"{user} [/INST] {assistant} <s>[INST] "
|
| 196 |
+
input_prompt += f"{message} [/INST] "
|
| 197 |
+
return input_prompt
|
| 198 |
+
|
| 199 |
+
## trust-game-llama-2-7b-chat
|
| 200 |
+
# app.py
|
| 201 |
@spaces.GPU
|
| 202 |
def generate(
|
|
|
|
| 203 |
message: str,
|
| 204 |
chat_history: list[tuple[str, str]],
|
| 205 |
+
# input_prompt: str,
|
| 206 |
max_new_tokens: int = 1024,
|
| 207 |
temperature: float = 0.6,
|
| 208 |
top_p: float = 0.9,
|
| 209 |
top_k: int = 50,
|
| 210 |
repetition_penalty: float = 1.2,
|
| 211 |
+
request: gr.Request, # To fetch query params
|
| 212 |
+
) -> Iterator[str]: # Change return type hint to Iterator[str]
|
| 213 |
|
| 214 |
+
conversation = []
|
| 215 |
+
|
| 216 |
# Fetch query params
|
| 217 |
params = request.query_params
|
| 218 |
print('those are the query params')
|
|
|
|
| 226 |
if session_index is not None:
|
| 227 |
print("Session index:", session_index)
|
| 228 |
else:
|
| 229 |
+
print("Session index not found or has no value.")
|
| 230 |
|
| 231 |
+
# Fetch personalized data
|
| 232 |
personalized_data = fetch_personalized_data(session_index)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 233 |
|
| 234 |
+
# Construct the input prompt using the functions from the system_prompt_config module
|
| 235 |
+
input_prompt = construct_input_prompt(chat_history, message, personalized_data)
|
| 236 |
+
|
| 237 |
# Move the condition here after the assignment
|
| 238 |
if input_prompt:
|
| 239 |
conversation.append({"role": "system", "content": input_prompt})
|
| 240 |
|
| 241 |
# Convert input prompt to tensor
|
| 242 |
input_ids = tokenizer(input_prompt, return_tensors="pt").to(model.device)
|
| 243 |
+
|
| 244 |
for user, assistant in chat_history:
|
| 245 |
conversation.extend([{"role": "user", "content": user}, {"role": "assistant", "content": assistant}])
|
| 246 |
conversation.append({"role": "user", "content": message})
|
|
|
|
| 252 |
input_ids = input_ids.to(model.device)
|
| 253 |
|
| 254 |
# Set up the TextIteratorStreamer
|
| 255 |
+
streamer = TextIteratorStreamer(tokenizer, timeout=10.0, skip_prompt=True, skip_special_tokens=True)
|
| 256 |
+
|
|
|
|
|
|
|
| 257 |
# Set up the generation arguments
|
| 258 |
generate_kwargs = dict(
|
| 259 |
{"input_ids": input_ids},
|
|
|
|
| 276 |
for text in streamer:
|
| 277 |
outputs.append(text)
|
| 278 |
yield "".join(outputs)
|
|
|
|
| 279 |
|
| 280 |
chat_interface = gr.ChatInterface(
|
| 281 |
fn=generate,
|