Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -14,12 +14,21 @@ indicators = ["Thinking ⠋", "Thinking ⠙", "Thinking ⠹", "Thinking ⠸", "T
|
|
| 14 |
|
| 15 |
@spaces.GPU(duration=120)
|
| 16 |
def generate_response(prompt, chat_history):
|
| 17 |
-
chat_history.append(
|
| 18 |
-
yield chat_history
|
| 19 |
|
| 20 |
print(chat_history)
|
| 21 |
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
model_inputs = tokenizer([text], return_tensors="pt").to(model.device)
|
| 25 |
|
|
@@ -42,7 +51,7 @@ def generate_response(prompt, chat_history):
|
|
| 42 |
reasoning_done = False
|
| 43 |
start_time = time.time()
|
| 44 |
|
| 45 |
-
chat_history.append(
|
| 46 |
|
| 47 |
indicator_index = 0
|
| 48 |
for new_text in streamer:
|
|
@@ -50,7 +59,7 @@ def generate_response(prompt, chat_history):
|
|
| 50 |
reasoning_done = True
|
| 51 |
thought_duration = time.time() - start_time
|
| 52 |
chat_history[-1]["metadata"] = {"title": f"Thought for {thought_duration:.2f} seconds"}
|
| 53 |
-
chat_history.append(
|
| 54 |
|
| 55 |
if not reasoning_done:
|
| 56 |
# Update the thinking indicator
|
|
@@ -62,14 +71,14 @@ def generate_response(prompt, chat_history):
|
|
| 62 |
content += new_text
|
| 63 |
chat_history[-1]["content"] = content
|
| 64 |
|
| 65 |
-
yield chat_history
|
| 66 |
|
| 67 |
# Create the Gradio interface
|
| 68 |
with gr.Blocks() as demo:
|
| 69 |
gr.Markdown("# Sarvam M Demo")
|
| 70 |
chatbot = gr.Chatbot(height=500, type="messages")
|
| 71 |
msg = gr.Textbox(label="Your Message")
|
| 72 |
-
msg.submit(generate_response, [msg, chatbot], [chatbot])
|
| 73 |
|
| 74 |
if __name__ == "__main__":
|
| 75 |
demo.launch(mcp_server=True)
|
|
|
|
| 14 |
|
| 15 |
@spaces.GPU(duration=120)
|
| 16 |
def generate_response(prompt, chat_history):
|
| 17 |
+
chat_history.append({"role": "user", "content": prompt})
|
| 18 |
+
yield chat_history, ""
|
| 19 |
|
| 20 |
print(chat_history)
|
| 21 |
|
| 22 |
+
# Preprocess chat history to include thinking tags
|
| 23 |
+
processed_chat_history = []
|
| 24 |
+
for message in chat_history:
|
| 25 |
+
# Skipping Thought Process in history
|
| 26 |
+
if message["role"] == "assistant" and "metadata" in message and message["metadata"].get("title", "").startswith("Thought"):
|
| 27 |
+
pass
|
| 28 |
+
else:
|
| 29 |
+
processed_chat_history.append(message)
|
| 30 |
+
|
| 31 |
+
text = tokenizer.apply_chat_template(processed_chat_history, tokenize=False, add_generation_prompt=True)
|
| 32 |
|
| 33 |
model_inputs = tokenizer([text], return_tensors="pt").to(model.device)
|
| 34 |
|
|
|
|
| 51 |
reasoning_done = False
|
| 52 |
start_time = time.time()
|
| 53 |
|
| 54 |
+
chat_history.append({"role": "assistant", "content": reasoning_content, "metadata": {"title": "Thinking..."}})
|
| 55 |
|
| 56 |
indicator_index = 0
|
| 57 |
for new_text in streamer:
|
|
|
|
| 59 |
reasoning_done = True
|
| 60 |
thought_duration = time.time() - start_time
|
| 61 |
chat_history[-1]["metadata"] = {"title": f"Thought for {thought_duration:.2f} seconds"}
|
| 62 |
+
chat_history.append({"role": "assistant", "content": content})
|
| 63 |
|
| 64 |
if not reasoning_done:
|
| 65 |
# Update the thinking indicator
|
|
|
|
| 71 |
content += new_text
|
| 72 |
chat_history[-1]["content"] = content
|
| 73 |
|
| 74 |
+
yield chat_history, ""
|
| 75 |
|
| 76 |
# Create the Gradio interface
|
| 77 |
with gr.Blocks() as demo:
|
| 78 |
gr.Markdown("# Sarvam M Demo")
|
| 79 |
chatbot = gr.Chatbot(height=500, type="messages")
|
| 80 |
msg = gr.Textbox(label="Your Message")
|
| 81 |
+
msg.submit(generate_response, [msg, chatbot], [chatbot, msg])
|
| 82 |
|
| 83 |
if __name__ == "__main__":
|
| 84 |
demo.launch(mcp_server=True)
|