Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
Upload folder using huggingface_hub
Browse files
app2.py
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import asyncio
|
| 2 |
+
from typing import List, AsyncIterator, TypedDict
|
| 3 |
+
|
| 4 |
+
from langgraph.graph import StateGraph, END
|
| 5 |
+
import gradio as gr
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
# 1. Define chat state
|
| 9 |
+
class ChatState(TypedDict):
|
| 10 |
+
messages: List[dict]
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
# 2. Respond Node — stream word by word, then ⏳
|
| 14 |
+
async def respond_node(state: ChatState) -> AsyncIterator[ChatState]:
|
| 15 |
+
messages = state["messages"]
|
| 16 |
+
user_msg = messages[-1]["content"]
|
| 17 |
+
reply = f"respond function: {user_msg}"
|
| 18 |
+
partial = ""
|
| 19 |
+
|
| 20 |
+
for word in reply.split():
|
| 21 |
+
partial += word + " "
|
| 22 |
+
await asyncio.sleep(0.3)
|
| 23 |
+
yield {"messages": messages + [{"role": "assistant", "content": partial.strip()}]}
|
| 24 |
+
|
| 25 |
+
# Add ⏳ and yield
|
| 26 |
+
final_msg = partial.strip()
|
| 27 |
+
messages.append({"role": "assistant", "content": final_msg + " ⏳"})
|
| 28 |
+
yield {"messages": messages}
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
# 3. Post-process node — replace ⏳ with ✅
|
| 32 |
+
async def post_process_node(state: ChatState) -> ChatState:
|
| 33 |
+
messages = state["messages"]
|
| 34 |
+
await asyncio.sleep(1.5)
|
| 35 |
+
|
| 36 |
+
if messages[-1]["role"] == "assistant" and "⏳" in messages[-1]["content"]:
|
| 37 |
+
messages[-1]["content"] = messages[-1]["content"].replace("⏳", "✅")
|
| 38 |
+
|
| 39 |
+
return {"messages": messages}
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
# 4. Define the graph
|
| 43 |
+
graph_builder = StateGraph(ChatState)
|
| 44 |
+
graph_builder.add_node("respond", respond_node)
|
| 45 |
+
graph_builder.add_node("post_process", post_process_node)
|
| 46 |
+
|
| 47 |
+
graph_builder.set_entry_point("respond")
|
| 48 |
+
graph_builder.add_edge("respond", "post_process")
|
| 49 |
+
graph_builder.add_edge("post_process", END)
|
| 50 |
+
|
| 51 |
+
graph = graph_builder.compile()
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
# 5. Gradio streaming handler
|
| 55 |
+
async def bot_respond_streaming(message: str, history: List[dict]) -> AsyncIterator:
|
| 56 |
+
state = {"messages": (history or []) + [{"role": "user", "content": message}]}
|
| 57 |
+
|
| 58 |
+
async for step in graph.astream(state):
|
| 59 |
+
# LangGraph yields steps like {"respond": ChatState, "post_process": ChatState}
|
| 60 |
+
for node_output in step.values():
|
| 61 |
+
yield node_output["messages"]
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
# 6. Gradio UI
|
| 65 |
+
with gr.Blocks() as demo:
|
| 66 |
+
chatbot = gr.Chatbot(label="Sanatan AI", type="messages")
|
| 67 |
+
with gr.Row():
|
| 68 |
+
textbox = gr.Textbox(placeholder="Ask something...", scale=8, container=False)
|
| 69 |
+
send_btn = gr.Button("Send", scale=1)
|
| 70 |
+
|
| 71 |
+
def user_submit(message, history):
|
| 72 |
+
return "", history + [{"role": "user", "content": message}]
|
| 73 |
+
|
| 74 |
+
send_event = send_btn.click(user_submit, [textbox, chatbot], [textbox, chatbot])
|
| 75 |
+
send_event.then(bot_respond_streaming, [textbox, chatbot], chatbot)
|
| 76 |
+
|
| 77 |
+
# Optional: allow enter to also send message
|
| 78 |
+
submit_event = textbox.submit(user_submit, [textbox, chatbot], [textbox, chatbot])
|
| 79 |
+
submit_event.then(bot_respond_streaming, [textbox, chatbot], chatbot)
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
if __name__ == "__main__":
|
| 83 |
+
demo.launch()
|