Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -10,12 +10,26 @@ from datetime import datetime
|
|
| 10 |
# π CONFIG
|
| 11 |
# =========================
|
| 12 |
|
|
|
|
| 13 |
os.environ["GOOGLE_API_KEY"] = "AIzaSyBzSyySlN8ELAY3cCZp3tzWTC3YHua1Ej0"
|
| 14 |
genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
|
| 15 |
model = genai.GenerativeModel("gemini-2.5-flash")
|
| 16 |
|
| 17 |
-
#
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
firebase_admin.initialize_app(cred)
|
| 20 |
db = firestore.client()
|
| 21 |
|
|
@@ -23,9 +37,11 @@ db = firestore.client()
|
|
| 23 |
# β‘ FASTAPI SETUP
|
| 24 |
# =========================
|
| 25 |
|
| 26 |
-
app = FastAPI(
|
| 27 |
-
|
| 28 |
-
|
|
|
|
|
|
|
| 29 |
|
| 30 |
app.add_middleware(
|
| 31 |
CORSMiddleware,
|
|
@@ -47,6 +63,7 @@ def get_user_chat_history(uid: str, session_id: str = "session_001"):
|
|
| 47 |
return doc.to_dict().get("history", [])
|
| 48 |
return []
|
| 49 |
|
|
|
|
| 50 |
def save_user_chat_history(uid: str, history: list, session_id: str = "session_001"):
|
| 51 |
"""Save chat history under user's subcollection."""
|
| 52 |
chat_doc = db.collection("users").document(uid).collection("chatbot_history").document(session_id)
|
|
@@ -116,6 +133,7 @@ def get_schemes(
|
|
| 116 |
data = get_farmer_schemes(query, uid, session_id)
|
| 117 |
return {"uid": uid, "session_id": session_id, "query": query, "schemes": data}
|
| 118 |
|
|
|
|
| 119 |
@app.get("/reset_chat")
|
| 120 |
def reset_chat(uid: str = Query(...), session_id: str = Query("session_001")):
|
| 121 |
db.collection("users").document(uid).collection("chatbot_history").document(session_id).delete()
|
|
@@ -127,5 +145,5 @@ def reset_chat(uid: str = Query(...), session_id: str = Query("session_001")):
|
|
| 127 |
|
| 128 |
if __name__ == "__main__":
|
| 129 |
import uvicorn
|
| 130 |
-
print("π Smart Farmer Advisor (Safe Memory) running at http://127.0.0.1:
|
| 131 |
-
uvicorn.run(app, host="0.0.0.0", port=
|
|
|
|
| 10 |
# π CONFIG
|
| 11 |
# =========================
|
| 12 |
|
| 13 |
+
# Configure Gemini API Key
|
| 14 |
os.environ["GOOGLE_API_KEY"] = "AIzaSyBzSyySlN8ELAY3cCZp3tzWTC3YHua1Ej0"
|
| 15 |
genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
|
| 16 |
model = genai.GenerativeModel("gemini-2.5-flash")
|
| 17 |
|
| 18 |
+
# =========================
|
| 19 |
+
# π₯ FIREBASE SETUP (with secret)
|
| 20 |
+
# =========================
|
| 21 |
+
|
| 22 |
+
# Try loading Firebase key from Hugging Face Secrets first
|
| 23 |
+
firebase_key_content = os.getenv("FIREBASE_KEY")
|
| 24 |
+
|
| 25 |
+
if firebase_key_content:
|
| 26 |
+
print("β
Using Firebase key from secret environment variable")
|
| 27 |
+
cred_dict = json.loads(firebase_key_content)
|
| 28 |
+
cred = credentials.Certificate(cred_dict)
|
| 29 |
+
else:
|
| 30 |
+
print("β οΈ Using local firebase_key.json (for local dev only)")
|
| 31 |
+
cred = credentials.Certificate("firebase_key.json")
|
| 32 |
+
|
| 33 |
firebase_admin.initialize_app(cred)
|
| 34 |
db = firestore.client()
|
| 35 |
|
|
|
|
| 37 |
# β‘ FASTAPI SETUP
|
| 38 |
# =========================
|
| 39 |
|
| 40 |
+
app = FastAPI(
|
| 41 |
+
title="πΎ Smart Farmer Advisor (Safe Memory)",
|
| 42 |
+
description="Gemini AI with persistent memory stored in user subcollection (safe for frontend)",
|
| 43 |
+
version="3.1"
|
| 44 |
+
)
|
| 45 |
|
| 46 |
app.add_middleware(
|
| 47 |
CORSMiddleware,
|
|
|
|
| 63 |
return doc.to_dict().get("history", [])
|
| 64 |
return []
|
| 65 |
|
| 66 |
+
|
| 67 |
def save_user_chat_history(uid: str, history: list, session_id: str = "session_001"):
|
| 68 |
"""Save chat history under user's subcollection."""
|
| 69 |
chat_doc = db.collection("users").document(uid).collection("chatbot_history").document(session_id)
|
|
|
|
| 133 |
data = get_farmer_schemes(query, uid, session_id)
|
| 134 |
return {"uid": uid, "session_id": session_id, "query": query, "schemes": data}
|
| 135 |
|
| 136 |
+
|
| 137 |
@app.get("/reset_chat")
|
| 138 |
def reset_chat(uid: str = Query(...), session_id: str = Query("session_001")):
|
| 139 |
db.collection("users").document(uid).collection("chatbot_history").document(session_id).delete()
|
|
|
|
| 145 |
|
| 146 |
if __name__ == "__main__":
|
| 147 |
import uvicorn
|
| 148 |
+
print("π Smart Farmer Advisor (Safe Memory) running at http://127.0.0.1:7860")
|
| 149 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|