Spaces:
Sleeping
Sleeping
added new features like view cache
Browse files
app.py
CHANGED
|
@@ -3,6 +3,7 @@ import gradio as gr
|
|
| 3 |
import redis
|
| 4 |
import numpy as np
|
| 5 |
import json
|
|
|
|
| 6 |
from openai import AzureOpenAI
|
| 7 |
from sentence_transformers import SentenceTransformer
|
| 8 |
|
|
@@ -15,10 +16,7 @@ redis_client = redis.Redis(
|
|
| 15 |
password=os.getenv("REDIS_PASSWORD")
|
| 16 |
)
|
| 17 |
|
| 18 |
-
#
|
| 19 |
-
# redis_client.flushdb()
|
| 20 |
-
|
| 21 |
-
# Azure OpenAI client (only for chat, not embeddings anymore)
|
| 22 |
client = AzureOpenAI(
|
| 23 |
api_key=os.getenv("AZURE_OPENAI_API_KEY").strip(),
|
| 24 |
api_version="2025-01-01-preview",
|
|
@@ -30,7 +28,10 @@ CHAT_DEPLOYMENT = "gpt-4.1"
|
|
| 30 |
# 🚀 Super lightweight multilingual embedding model
|
| 31 |
embedder = SentenceTransformer("intfloat/multilingual-e5-small")
|
| 32 |
|
| 33 |
-
#
|
|
|
|
|
|
|
|
|
|
| 34 |
def get_embedding(text):
|
| 35 |
return embedder.encode(text, convert_to_numpy=True).astype(np.float32)
|
| 36 |
|
|
@@ -61,11 +62,24 @@ def store_cache(user_id, user_input, output):
|
|
| 61 |
"embedding": vec,
|
| 62 |
"output": output
|
| 63 |
}))
|
|
|
|
|
|
|
| 64 |
|
| 65 |
def clear_user_cache(user_id):
|
| 66 |
cache_key = f"cache:{user_id}"
|
| 67 |
redis_client.delete(cache_key)
|
| 68 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
def chat_with_ai(user_id, user_input):
|
| 70 |
if not user_input:
|
| 71 |
return "Please type something."
|
|
@@ -91,9 +105,13 @@ def chat_with_ai(user_id, user_input):
|
|
| 91 |
|
| 92 |
# Gradio UI
|
| 93 |
with gr.Blocks(title="Azure OpenAI + Redis Cloud Chat") as demo:
|
| 94 |
-
gr.Markdown("# 💬 Azure OpenAI + Redis Cloud (Semantic Cache, Multi-User)")
|
| 95 |
|
| 96 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
|
| 98 |
with gr.Row():
|
| 99 |
chatbot = gr.Chatbot(type="messages")
|
|
@@ -101,21 +119,39 @@ with gr.Blocks(title="Azure OpenAI + Redis Cloud Chat") as demo:
|
|
| 101 |
with gr.Row():
|
| 102 |
msg = gr.Textbox(placeholder="Type your message here...")
|
| 103 |
send = gr.Button("Send")
|
|
|
|
|
|
|
| 104 |
clear = gr.Button("🧹 Clear Cache")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
|
| 106 |
def respond(message, history, user_id):
|
|
|
|
|
|
|
| 107 |
bot_reply = chat_with_ai(user_id, message)
|
| 108 |
history.append({"role": "user", "content": message})
|
| 109 |
history.append({"role": "assistant", "content": bot_reply})
|
| 110 |
return history, ""
|
| 111 |
|
| 112 |
def clear_cache_ui(user_id, history):
|
|
|
|
|
|
|
| 113 |
clear_user_cache(user_id)
|
| 114 |
return [], f"✅ Cache cleared for {user_id}"
|
| 115 |
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 119 |
|
| 120 |
if __name__ == "__main__":
|
| 121 |
demo.launch(server_name="0.0.0.0", server_port=7860, debug=True, pwa=True)
|
|
|
|
| 3 |
import redis
|
| 4 |
import numpy as np
|
| 5 |
import json
|
| 6 |
+
from datetime import timedelta
|
| 7 |
from openai import AzureOpenAI
|
| 8 |
from sentence_transformers import SentenceTransformer
|
| 9 |
|
|
|
|
| 16 |
password=os.getenv("REDIS_PASSWORD")
|
| 17 |
)
|
| 18 |
|
| 19 |
+
# Azure OpenAI client
|
|
|
|
|
|
|
|
|
|
| 20 |
client = AzureOpenAI(
|
| 21 |
api_key=os.getenv("AZURE_OPENAI_API_KEY").strip(),
|
| 22 |
api_version="2025-01-01-preview",
|
|
|
|
| 28 |
# 🚀 Super lightweight multilingual embedding model
|
| 29 |
embedder = SentenceTransformer("intfloat/multilingual-e5-small")
|
| 30 |
|
| 31 |
+
# Cache expiration: 2 days (in seconds)
|
| 32 |
+
CACHE_TTL = int(timedelta(days=2).total_seconds())
|
| 33 |
+
|
| 34 |
+
# Helper: get embedding
|
| 35 |
def get_embedding(text):
|
| 36 |
return embedder.encode(text, convert_to_numpy=True).astype(np.float32)
|
| 37 |
|
|
|
|
| 62 |
"embedding": vec,
|
| 63 |
"output": output
|
| 64 |
}))
|
| 65 |
+
# ⏳ Reset TTL every time user stores something
|
| 66 |
+
redis_client.expire(cache_key, CACHE_TTL)
|
| 67 |
|
| 68 |
def clear_user_cache(user_id):
|
| 69 |
cache_key = f"cache:{user_id}"
|
| 70 |
redis_client.delete(cache_key)
|
| 71 |
|
| 72 |
+
def view_user_cache(user_id):
|
| 73 |
+
cache_key = f"cache:{user_id}"
|
| 74 |
+
entries = redis_client.hgetall(cache_key)
|
| 75 |
+
if not entries:
|
| 76 |
+
return "⚠️ No cache stored."
|
| 77 |
+
lines = []
|
| 78 |
+
for q, val in entries.items():
|
| 79 |
+
entry = json.loads(val)
|
| 80 |
+
lines.append(f"**Q:** {q}\n**A:** {entry['output']}")
|
| 81 |
+
return "\n\n---\n\n".join(lines)
|
| 82 |
+
|
| 83 |
def chat_with_ai(user_id, user_input):
|
| 84 |
if not user_input:
|
| 85 |
return "Please type something."
|
|
|
|
| 105 |
|
| 106 |
# Gradio UI
|
| 107 |
with gr.Blocks(title="Azure OpenAI + Redis Cloud Chat") as demo:
|
| 108 |
+
gr.Markdown("# 💬 Azure OpenAI + Redis Cloud (Semantic Cache, Multi-User, Auto Clean)")
|
| 109 |
|
| 110 |
+
user_id_state = gr.State("")
|
| 111 |
+
|
| 112 |
+
with gr.Row():
|
| 113 |
+
user_id_input = gr.Textbox(label="Enter Username (only once)", placeholder="Your username")
|
| 114 |
+
save_user = gr.Button("✅ Save Username")
|
| 115 |
|
| 116 |
with gr.Row():
|
| 117 |
chatbot = gr.Chatbot(type="messages")
|
|
|
|
| 119 |
with gr.Row():
|
| 120 |
msg = gr.Textbox(placeholder="Type your message here...")
|
| 121 |
send = gr.Button("Send")
|
| 122 |
+
|
| 123 |
+
with gr.Row():
|
| 124 |
clear = gr.Button("🧹 Clear Cache")
|
| 125 |
+
view = gr.Button("👀 View Cache")
|
| 126 |
+
cache_output = gr.Markdown("")
|
| 127 |
+
|
| 128 |
+
def set_user_id(uid):
|
| 129 |
+
return uid, f"✅ Username set as **{uid}**"
|
| 130 |
|
| 131 |
def respond(message, history, user_id):
|
| 132 |
+
if not user_id:
|
| 133 |
+
return history, "⚠️ Please set username first!"
|
| 134 |
bot_reply = chat_with_ai(user_id, message)
|
| 135 |
history.append({"role": "user", "content": message})
|
| 136 |
history.append({"role": "assistant", "content": bot_reply})
|
| 137 |
return history, ""
|
| 138 |
|
| 139 |
def clear_cache_ui(user_id, history):
|
| 140 |
+
if not user_id:
|
| 141 |
+
return history, "⚠️ Please set username first!"
|
| 142 |
clear_user_cache(user_id)
|
| 143 |
return [], f"✅ Cache cleared for {user_id}"
|
| 144 |
|
| 145 |
+
def view_cache_ui(user_id):
|
| 146 |
+
if not user_id:
|
| 147 |
+
return "⚠️ Please set username first!"
|
| 148 |
+
return view_user_cache(user_id)
|
| 149 |
+
|
| 150 |
+
save_user.click(set_user_id, user_id_input, [user_id_state, cache_output])
|
| 151 |
+
send.click(respond, [msg, chatbot, user_id_state], [chatbot, msg])
|
| 152 |
+
msg.submit(respond, [msg, chatbot, user_id_state], [chatbot, msg])
|
| 153 |
+
clear.click(clear_cache_ui, [user_id_state, chatbot], [chatbot, cache_output])
|
| 154 |
+
view.click(view_cache_ui, user_id_state, cache_output)
|
| 155 |
|
| 156 |
if __name__ == "__main__":
|
| 157 |
demo.launch(server_name="0.0.0.0", server_port=7860, debug=True, pwa=True)
|