Spaces:
Running
Running
Upload folder using huggingface_hub
Browse files
app.py
CHANGED
|
@@ -16,11 +16,11 @@ graph = generate_graph()
|
|
| 16 |
|
| 17 |
def init():
|
| 18 |
load_dotenv(override=True)
|
| 19 |
-
|
| 20 |
if(os.path.isdir("./chromadb-store")):
|
| 21 |
logger.warning("database exists locally. not downloading.")
|
| 22 |
return
|
| 23 |
-
|
| 24 |
downloader = ZipDownloader(
|
| 25 |
service_account_json=os.getenv("GOOGLE_SERVICE_ACCOUNT_JSON")
|
| 26 |
)
|
|
@@ -39,29 +39,6 @@ def init_session():
|
|
| 39 |
return str(uuid.uuid4())
|
| 40 |
|
| 41 |
|
| 42 |
-
def chat_stream(message, history, thread_id):
|
| 43 |
-
print("received chat message for thread:", thread_id)
|
| 44 |
-
config = {"configurable": {"thread_id": thread_id}}
|
| 45 |
-
|
| 46 |
-
# Start the stream
|
| 47 |
-
stream = graph.stream(
|
| 48 |
-
{"messages": [{"role": "user", "content": message}]}, config=config
|
| 49 |
-
)
|
| 50 |
-
|
| 51 |
-
# Loop through the stream and yield what's happening
|
| 52 |
-
for event in stream:
|
| 53 |
-
step = event["step"].name
|
| 54 |
-
yield f"**Step:** `{step}`\n"
|
| 55 |
-
|
| 56 |
-
if "output" in event:
|
| 57 |
-
messages = event["output"].get("messages", [])
|
| 58 |
-
if messages:
|
| 59 |
-
# Only yield the *new* message from the step
|
| 60 |
-
latest = messages[-1]
|
| 61 |
-
if hasattr(latest, "content"):
|
| 62 |
-
yield latest.content
|
| 63 |
-
|
| 64 |
-
|
| 65 |
def chat(message, history, thread_id):
|
| 66 |
print("received chat message for thread:", thread_id)
|
| 67 |
config = {"configurable": {"thread_id": thread_id}}
|
|
@@ -108,75 +85,11 @@ chatbot = gr.Chatbot(
|
|
| 108 |
show_copy_all_button=True,
|
| 109 |
type="messages",
|
| 110 |
height=700,
|
| 111 |
-
render_markdown=True
|
| 112 |
)
|
| 113 |
chatInterface = gr.ChatInterface(
|
| 114 |
-
title="Sanatan-AI",
|
| 115 |
-
fn=chat_stream,
|
| 116 |
-
additional_inputs=[thread_id],
|
| 117 |
-
chatbot=chatbot
|
| 118 |
)
|
| 119 |
# initializze database
|
| 120 |
init()
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
with gr.Blocks() as demo:
|
| 124 |
-
thread_id = gr.State(init_session)
|
| 125 |
-
|
| 126 |
-
chatbot = gr.Chatbot(
|
| 127 |
-
label="Sanatan-Bot",
|
| 128 |
-
avatar_images=("assets/avatar_user.png", "assets/adiyen_bot.png"),
|
| 129 |
-
value=intro_messages.copy(),
|
| 130 |
-
type="messages",
|
| 131 |
-
height=700,
|
| 132 |
-
show_copy_button=True,
|
| 133 |
-
show_copy_all_button=True,
|
| 134 |
-
render_markdown=True,
|
| 135 |
-
)
|
| 136 |
-
trace_output = gr.Textbox(label="LangGraph Trace", lines=10, interactive=False)
|
| 137 |
-
msg = gr.Textbox(label="Ask your question")
|
| 138 |
-
send_btn = gr.Button("Send")
|
| 139 |
-
def chat_streaming(message, chat_history, thread_id):
|
| 140 |
-
config = {"configurable": {"thread_id": thread_id}}
|
| 141 |
-
stream = graph.stream({"messages": [{"role": "user", "content": message}]}, config=config)
|
| 142 |
-
|
| 143 |
-
# Add user message first
|
| 144 |
-
chat_history = chat_history + [{"role": "user", "content": message}]
|
| 145 |
-
yield chat_history, gr.update()
|
| 146 |
-
|
| 147 |
-
trace_lines = []
|
| 148 |
-
|
| 149 |
-
for event in stream:
|
| 150 |
-
if event.get("type") == "step":
|
| 151 |
-
step = event["step"].name
|
| 152 |
-
trace_lines.append(f"➡️ Step: `{step}`")
|
| 153 |
-
yield chat_history, gr.Textbox.update(value="\n".join(trace_lines))
|
| 154 |
-
|
| 155 |
-
output = event.get("output", {})
|
| 156 |
-
messages = output.get("messages", [])
|
| 157 |
-
if messages:
|
| 158 |
-
latest = messages[-1]
|
| 159 |
-
if isinstance(latest, dict):
|
| 160 |
-
content = latest.get("content", "")
|
| 161 |
-
else:
|
| 162 |
-
content = getattr(latest, "content", "")
|
| 163 |
-
|
| 164 |
-
if content:
|
| 165 |
-
# Add assistant message progressively
|
| 166 |
-
chat_history = chat_history + [{"role": "assistant", "content": content}]
|
| 167 |
-
yield chat_history, gr.Textbox.update(value="\n".join(trace_lines))
|
| 168 |
-
|
| 169 |
-
elif event.get("type") == "end":
|
| 170 |
-
logger.info("All Done!")
|
| 171 |
-
trace_lines.append("✅ Done.")
|
| 172 |
-
yield chat_history, gr.Textbox.update(value="\n".join(trace_lines))
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
send_btn.click(
|
| 176 |
-
fn=chat_streaming,
|
| 177 |
-
inputs=[msg, chatbot, thread_id],
|
| 178 |
-
outputs=[chatbot, trace_output],
|
| 179 |
-
)
|
| 180 |
-
|
| 181 |
-
demo.queue()
|
| 182 |
-
demo.launch()
|
|
|
|
| 16 |
|
| 17 |
def init():
|
| 18 |
load_dotenv(override=True)
|
| 19 |
+
|
| 20 |
if(os.path.isdir("./chromadb-store")):
|
| 21 |
logger.warning("database exists locally. not downloading.")
|
| 22 |
return
|
| 23 |
+
|
| 24 |
downloader = ZipDownloader(
|
| 25 |
service_account_json=os.getenv("GOOGLE_SERVICE_ACCOUNT_JSON")
|
| 26 |
)
|
|
|
|
| 39 |
return str(uuid.uuid4())
|
| 40 |
|
| 41 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
def chat(message, history, thread_id):
|
| 43 |
print("received chat message for thread:", thread_id)
|
| 44 |
config = {"configurable": {"thread_id": thread_id}}
|
|
|
|
| 85 |
show_copy_all_button=True,
|
| 86 |
type="messages",
|
| 87 |
height=700,
|
| 88 |
+
render_markdown=True,
|
| 89 |
)
|
| 90 |
chatInterface = gr.ChatInterface(
|
| 91 |
+
title="Sanatan-AI", fn=chat, additional_inputs=[thread_id], chatbot=chatbot
|
|
|
|
|
|
|
|
|
|
| 92 |
)
|
| 93 |
# initializze database
|
| 94 |
init()
|
| 95 |
+
chatInterface.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|