Spaces:
Sleeping
Sleeping
fix
Browse files
app.py
CHANGED
@@ -44,27 +44,22 @@ def generate(
|
|
44 |
top_p: float = 0.9,
|
45 |
top_k: int = 50,
|
46 |
repetition_penalty: float = 1.1,
|
47 |
-
) ->
|
48 |
-
# Ensure we have valid chat history
|
49 |
if chat_history is None:
|
50 |
chat_history = []
|
51 |
|
52 |
-
# Build conversation context
|
53 |
conversation = []
|
54 |
if system_prompt:
|
55 |
conversation.append({"role": "system", "content": system_prompt})
|
56 |
|
57 |
-
# Add all previous exchanges
|
58 |
for user, assistant in chat_history:
|
59 |
conversation.extend([
|
60 |
{"role": "user", "content": str(user).strip()},
|
61 |
{"role": "assistant", "content": str(assistant).strip()}
|
62 |
])
|
63 |
|
64 |
-
# Add current message
|
65 |
conversation.append({"role": "user", "content": str(message).strip()})
|
66 |
|
67 |
-
# Generate response
|
68 |
try:
|
69 |
input_ids = tokenizer.apply_chat_template(conversation, return_tensors="pt")
|
70 |
if input_ids.shape[1] > MAX_INPUT_TOKEN_LENGTH:
|
@@ -91,11 +86,14 @@ def generate(
|
|
91 |
outputs = []
|
92 |
for text in streamer:
|
93 |
outputs.append(text)
|
94 |
-
|
|
|
|
|
95 |
|
96 |
except Exception as e:
|
97 |
gr.Warning(f"Error during generation: {str(e)}")
|
98 |
-
|
|
|
99 |
|
100 |
def create_demo() -> gr.Blocks:
|
101 |
with gr.Blocks(css="style.css") as demo:
|
@@ -173,8 +171,8 @@ def create_demo() -> gr.Blocks:
|
|
173 |
["How does Kant's Categorical Imperative work?"],
|
174 |
["What is the problem of consciousness in philosophy of mind?"],
|
175 |
],
|
176 |
-
inputs=msg,
|
177 |
-
fn=generate,
|
178 |
outputs=chatbot,
|
179 |
cache_examples=True,
|
180 |
api_name=False
|
|
|
44 |
top_p: float = 0.9,
|
45 |
top_k: int = 50,
|
46 |
repetition_penalty: float = 1.1,
|
47 |
+
) -> list[tuple[str, str]]:
|
|
|
48 |
if chat_history is None:
|
49 |
chat_history = []
|
50 |
|
|
|
51 |
conversation = []
|
52 |
if system_prompt:
|
53 |
conversation.append({"role": "system", "content": system_prompt})
|
54 |
|
|
|
55 |
for user, assistant in chat_history:
|
56 |
conversation.extend([
|
57 |
{"role": "user", "content": str(user).strip()},
|
58 |
{"role": "assistant", "content": str(assistant).strip()}
|
59 |
])
|
60 |
|
|
|
61 |
conversation.append({"role": "user", "content": str(message).strip()})
|
62 |
|
|
|
63 |
try:
|
64 |
input_ids = tokenizer.apply_chat_template(conversation, return_tensors="pt")
|
65 |
if input_ids.shape[1] > MAX_INPUT_TOKEN_LENGTH:
|
|
|
86 |
outputs = []
|
87 |
for text in streamer:
|
88 |
outputs.append(text)
|
89 |
+
partial_output = "".join(outputs)
|
90 |
+
chat_history = chat_history + [(message, partial_output)]
|
91 |
+
yield chat_history
|
92 |
|
93 |
except Exception as e:
|
94 |
gr.Warning(f"Error during generation: {str(e)}")
|
95 |
+
chat_history = chat_history + [(message, "I apologize, but I encountered an error. Please try again.")]
|
96 |
+
yield chat_history
|
97 |
|
98 |
def create_demo() -> gr.Blocks:
|
99 |
with gr.Blocks(css="style.css") as demo:
|
|
|
171 |
["How does Kant's Categorical Imperative work?"],
|
172 |
["What is the problem of consciousness in philosophy of mind?"],
|
173 |
],
|
174 |
+
inputs=[msg],
|
175 |
+
fn=lambda x: generate(x, [], system_prompt.value, max_new_tokens.value, temperature.value, top_p.value, top_k.value, repetition_penalty.value),
|
176 |
outputs=chatbot,
|
177 |
cache_examples=True,
|
178 |
api_name=False
|