Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
@@ -4,6 +4,9 @@
|
|
4 |
import os, time, importlib.util
|
5 |
import gradio as gr
|
6 |
from huggingface_hub import hf_hub_download
|
|
|
|
|
|
|
7 |
|
8 |
# ----------------------------------------------------------------------
|
9 |
# Helper to read secrets from the HF Space environment
|
@@ -20,9 +23,12 @@ def _secret(key: str, fallback: str = None) -> str:
|
|
20 |
# 1. Configuration & Constants
|
21 |
# ----------------------------------------------------------------------
|
22 |
REPO_ID = _secret("REPO_ID")
|
23 |
-
FILES_TO_DOWNLOAD = ["index.faiss", "index.pkl", "agent_logic.py"]
|
24 |
LOCAL_DOWNLOAD_DIR = "downloaded_assets"
|
25 |
EMBEDDING_MODEL_NAME = "google/embeddinggemma-300m"
|
|
|
|
|
|
|
26 |
|
27 |
# ----------------------------------------------------------------------
|
28 |
# 2. Bootstrap Phase – download assets and import the RAG engine
|
@@ -62,14 +68,30 @@ def respond(message: str, history: list[dict[str, str]]):
|
|
62 |
Called by Gradio for each user message.
|
63 |
Streams the response back to the UI.
|
64 |
"""
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
73 |
|
74 |
# ----------------------------------------------------------------------
|
75 |
# 4. UI Layout – Tips + Chat + Footer
|
@@ -82,7 +104,7 @@ tips_md = r"""
|
|
82 |
AI Agent yang menggunakan Retrieval‑Augmented Generation (RAG) untuk menjawab pertanyaan dari dokumentasi PRECISE (disimpan dalam FAISS storage).
|
83 |
- **Perbedaan dengan chatbot sebelumnya :**
|
84 |
• Dengan menggunakan agentic RAG, agent hanya mengambil dokumentasi yang dibutuhkan.
|
85 |
-
• Karena efisiensi konteks dan efisiensi design, dapat menggunakan model kecerdasan rendah, sehingga cost turun sekitar
|
86 |
- **Tips untuk menggunakan**
|
87 |
• Usahakan pertanyaan Anda spesifik agar jawaban lebih akurat.
|
88 |
• Jika jawaban kurang mengena, coba reset chat atau tanyakan ulang dengan bahasa berbeda.
|
@@ -94,9 +116,9 @@ tips_md = r"""
|
|
94 |
# 4.2 Footer – the old description / notes
|
95 |
footer_md = r"""
|
96 |
---
|
97 |
-
**
|
98 |
-
**
|
99 |
-
*Updated
|
100 |
|
101 |
"""
|
102 |
|
@@ -104,7 +126,7 @@ footer_md = r"""
|
|
104 |
chatbot = gr.ChatInterface(
|
105 |
respond,
|
106 |
type="messages",
|
107 |
-
title="PRECISE RAG Agent",
|
108 |
examples=[
|
109 |
["Jelaskan konsep PRECISE secara sederhana."],
|
110 |
["Berapa keuntungan finansial yang didapat menggunakan PRECISE?"],
|
|
|
4 |
import os, time, importlib.util
|
5 |
import gradio as gr
|
6 |
from huggingface_hub import hf_hub_download
|
7 |
+
from datetime import datetime
|
8 |
+
from datetime import date
|
9 |
+
from upstash_redis import Redis
|
10 |
|
11 |
# ----------------------------------------------------------------------
|
12 |
# Helper to read secrets from the HF Space environment
|
|
|
23 |
# 1. Configuration & Constants
|
24 |
# ----------------------------------------------------------------------
|
25 |
REPO_ID = _secret("REPO_ID")
|
26 |
+
FILES_TO_DOWNLOAD = ["index.faiss", "index.pkl", "agent_logic.py","prec_hyde_agent.txt","prec_rag_agent.txt"]
|
27 |
LOCAL_DOWNLOAD_DIR = "downloaded_assets"
|
28 |
EMBEDDING_MODEL_NAME = "google/embeddinggemma-300m"
|
29 |
+
redis = Redis(url=_secret("UPSTASH_REDIS_URL")
|
30 |
+
, token= _secret("UPSTASH_TOKEN")
|
31 |
+
)
|
32 |
|
33 |
# ----------------------------------------------------------------------
|
34 |
# 2. Bootstrap Phase – download assets and import the RAG engine
|
|
|
68 |
Called by Gradio for each user message.
|
69 |
Streams the response back to the UI.
|
70 |
"""
|
71 |
+
try:
|
72 |
+
# Check expiration
|
73 |
+
end_date = datetime.strptime(_secret("END_DATE"), "%Y-%m-%d").date()
|
74 |
+
if date.today() > end_date:
|
75 |
+
return "Chatbot sudah expired." # Direct return for errors
|
76 |
+
|
77 |
+
# Check request limit
|
78 |
+
remaining_requests = redis.decr("request_limit")
|
79 |
+
if remaining_requests < 0:
|
80 |
+
return "Kuota chat sudah habis." # Direct return for errors
|
81 |
+
|
82 |
+
# If we pass all checks, then stream the response
|
83 |
+
final_response = engine.get_response(message, history)
|
84 |
+
|
85 |
+
# Stream the response with typing effect
|
86 |
+
response = ""
|
87 |
+
for char in final_response:
|
88 |
+
response += char
|
89 |
+
time.sleep(0.01)
|
90 |
+
yield response
|
91 |
+
|
92 |
+
except Exception as e:
|
93 |
+
print(f"Error in respond function: {e}")
|
94 |
+
return "Terjadi error saat memproses permintaan. Silakan coba lagi."
|
95 |
|
96 |
# ----------------------------------------------------------------------
|
97 |
# 4. UI Layout – Tips + Chat + Footer
|
|
|
104 |
AI Agent yang menggunakan Retrieval‑Augmented Generation (RAG) untuk menjawab pertanyaan dari dokumentasi PRECISE (disimpan dalam FAISS storage).
|
105 |
- **Perbedaan dengan chatbot sebelumnya :**
|
106 |
• Dengan menggunakan agentic RAG, agent hanya mengambil dokumentasi yang dibutuhkan.
|
107 |
+
• Karena efisiensi konteks dan efisiensi design, dapat menggunakan model kecerdasan rendah, sehingga cost turun sekitar 95% dibanding versi non RAG yang sebelumnya.
|
108 |
- **Tips untuk menggunakan**
|
109 |
• Usahakan pertanyaan Anda spesifik agar jawaban lebih akurat.
|
110 |
• Jika jawaban kurang mengena, coba reset chat atau tanyakan ulang dengan bahasa berbeda.
|
|
|
116 |
# 4.2 Footer – the old description / notes
|
117 |
footer_md = r"""
|
118 |
---
|
119 |
+
**Komponen**: LangChain + FAISS + Redis
|
120 |
+
**Models**: Qwen3-4B-Thinking-2507, Qwen3-4B-Instruct-2507
|
121 |
+
*Updated 25 Sep 2025 – YOI*
|
122 |
|
123 |
"""
|
124 |
|
|
|
126 |
chatbot = gr.ChatInterface(
|
127 |
respond,
|
128 |
type="messages",
|
129 |
+
title="PRECISE RAG Agent (Expired 1 April 2026)",
|
130 |
examples=[
|
131 |
["Jelaskan konsep PRECISE secara sederhana."],
|
132 |
["Berapa keuntungan finansial yang didapat menggunakan PRECISE?"],
|