Spaces:
Build error
Build error
jakewatson commited on
Commit ·
fd18cd5
1
Parent(s): 958dc04
fixing request duration
Browse files
app.py
CHANGED
|
@@ -41,54 +41,49 @@ def respond(
|
|
| 41 |
global stop_inference
|
| 42 |
stop_inference = False # Reset cancellation flag
|
| 43 |
REQUEST_COUNTER.inc() # Increment request counter
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
history
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
# Use local model (Phi-3-mini-4k-instruct)
|
| 60 |
-
prompt = local_pipe.tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
|
| 61 |
-
output = local_pipe(
|
| 62 |
-
prompt,
|
| 63 |
-
do_sample=True
|
| 64 |
-
)
|
| 65 |
-
response_text = output[0]["generated_text"].split("<|assistant|>")[-1].strip()
|
| 66 |
-
|
| 67 |
-
else:
|
| 68 |
-
# Use API-based model (Zephyr 7B)
|
| 69 |
-
response_text = ""
|
| 70 |
try:
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
except Exception as e:
|
|
|
|
| 77 |
print(f"Error in API response: {e}")
|
| 78 |
response_text = "Error generating response"
|
|
|
|
|
|
|
| 79 |
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
try:
|
| 84 |
-
SUCCESSFUL_REQUESTS.inc() # Increment successful request counter
|
| 85 |
-
except Exception as e:
|
| 86 |
-
FAILED_REQUESTS.inc() # Increment failed request counter
|
| 87 |
-
yield history + [(message, f"Error: {str(e)}")]
|
| 88 |
-
finally:
|
| 89 |
-
request_timer.observe_duration() # Stop timing the request
|
| 90 |
-
|
| 91 |
-
return history
|
| 92 |
|
| 93 |
def cancel_inference():
|
| 94 |
global stop_inference
|
|
|
|
| 41 |
global stop_inference
|
| 42 |
stop_inference = False # Reset cancellation flag
|
| 43 |
REQUEST_COUNTER.inc() # Increment request counter
|
| 44 |
+
|
| 45 |
+
# Start timing the request
|
| 46 |
+
with REQUEST_DURATION.time():
|
| 47 |
+
# Initialize history if it's None
|
| 48 |
+
if history is None:
|
| 49 |
+
history = []
|
| 50 |
+
|
| 51 |
+
# Prepare the chat messages with the system message and conversation history
|
| 52 |
+
messages = [{"role": "system", "content": system_message}]
|
| 53 |
+
for user_input, bot_response in history:
|
| 54 |
+
messages.append({"role": "user", "content": user_input})
|
| 55 |
+
messages.append({"role": "assistant", "content": bot_response})
|
| 56 |
+
messages.append({"role": "user", "content": message})
|
| 57 |
+
|
| 58 |
+
# Generate response based on the model selected
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
try:
|
| 60 |
+
if use_local_model:
|
| 61 |
+
# Use local model (Phi-3-mini-4k-instruct)
|
| 62 |
+
prompt = local_pipe.tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
|
| 63 |
+
output = local_pipe(
|
| 64 |
+
prompt,
|
| 65 |
+
do_sample=True
|
| 66 |
+
)
|
| 67 |
+
response_text = output[0]["generated_text"].split("<|assistant|>")[-1].strip()
|
| 68 |
+
else:
|
| 69 |
+
# Use API-based model (Zephyr 7B)
|
| 70 |
+
response = client.chat_completion(
|
| 71 |
+
messages=messages,
|
| 72 |
+
stream=False
|
| 73 |
+
)
|
| 74 |
+
response_text = response['choices'][0]['message']['content']
|
| 75 |
+
SUCCESSFUL_REQUESTS.inc() # Increment successful request counter
|
| 76 |
+
|
| 77 |
except Exception as e:
|
| 78 |
+
FAILED_REQUESTS.inc() # Increment failed request counter
|
| 79 |
print(f"Error in API response: {e}")
|
| 80 |
response_text = "Error generating response"
|
| 81 |
+
history.append((message, response_text))
|
| 82 |
+
return history # Return history with error message if an exception occurs
|
| 83 |
|
| 84 |
+
# Append the user message and model response to history
|
| 85 |
+
history.append((message, response_text))
|
| 86 |
+
return history
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
|
| 88 |
def cancel_inference():
|
| 89 |
global stop_inference
|