Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,116 +1,103 @@
|
|
1 |
from fastapi import FastAPI
|
2 |
from fastapi.middleware.cors import CORSMiddleware
|
3 |
-
from fastapi.staticfiles import StaticFiles
|
4 |
from fastapi.responses import HTMLResponse
|
|
|
5 |
import gradio as gr
|
6 |
import numpy as np
|
7 |
import faiss
|
8 |
import logging
|
|
|
9 |
import requests
|
10 |
import json
|
11 |
-
|
12 |
-
url = "https://api.fireworks.ai/inference/v1/chat/completions"
|
13 |
-
payload = {
|
14 |
-
"model": "accounts/fireworks/models/deepseek-r1",
|
15 |
-
"max_tokens": 4096,
|
16 |
-
"top_p": 1,
|
17 |
-
"top_k": 40,
|
18 |
-
"presence_penalty": 0,
|
19 |
-
"frequency_penalty": 0,
|
20 |
-
"temperature": 0.6,
|
21 |
-
"messages": []
|
22 |
-
}
|
23 |
-
headers = {
|
24 |
-
"Accept": "application/json",
|
25 |
-
"Content-Type": "application/json",
|
26 |
-
"Authorization": "Bearer <API_KEY>"
|
27 |
-
}
|
28 |
-
requests.request("POST", url, headers=headers, data=json.dumps(payload))
|
29 |
-
|
30 |
-
import os
|
31 |
-
os.environ["TRANSFORMERS_CACHE"] = "/app/cache/transformers"
|
32 |
-
os.environ["HF_HOME"] = "/app/cache/hf"
|
33 |
-
os.environ["SENTENCE_TRANSFORMERS_HOME"] = "/app/cache/st"
|
34 |
-
|
35 |
from sentence_transformers import SentenceTransformer
|
36 |
-
model = SentenceTransformer("sentence-transformers/all-MiniLM-L6-v2")
|
37 |
-
logging.basicConfig(level=logging.INFO) # No file, just console
|
38 |
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
)
|
44 |
|
45 |
-
#
|
46 |
-
|
47 |
-
model = SentenceTransformer('all-MiniLM-L6-v2')
|
48 |
index = faiss.IndexFlatL2(384)
|
49 |
memory_text = []
|
50 |
|
51 |
-
#
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
60 |
def autonomous_agent(input_text):
|
61 |
-
vec = model.encode([input_text])[0]
|
62 |
response = ""
|
63 |
|
64 |
if index.ntotal > 0:
|
65 |
-
D, I = index.search(np.array([vec]), min(5, index.ntotal))
|
66 |
-
matches = []
|
67 |
-
|
68 |
-
for idx, dist in zip(I[0], D[0]):
|
69 |
-
if idx != -1 and dist < 0.8:
|
70 |
-
matches.append(memory_text[idx])
|
71 |
-
|
72 |
if matches:
|
73 |
-
response
|
74 |
-
else:
|
75 |
-
response = "🤖 No relevant memories found"
|
76 |
-
else:
|
77 |
-
response = "🤖 Memory is empty"
|
78 |
|
79 |
-
# Store
|
80 |
index.add(np.array([vec]))
|
81 |
memory_text.append(input_text)
|
82 |
|
|
|
|
|
|
|
83 |
return response
|
84 |
|
85 |
-
# Gradio
|
86 |
gradio_ui = gr.Interface(
|
87 |
fn=autonomous_agent,
|
88 |
inputs="text",
|
89 |
outputs="text",
|
90 |
title="Autonomous AI Agent",
|
91 |
-
description="Self-enhancing chatbot with
|
92 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
93 |
)
|
94 |
|
95 |
-
#
|
96 |
@app.get("/", response_class=HTMLResponse)
|
97 |
async def root():
|
98 |
return """
|
99 |
<html>
|
100 |
-
<head>
|
101 |
-
<title>Autonomous AI Agent</title>
|
102 |
-
</head>
|
103 |
<body>
|
104 |
-
<
|
105 |
-
<iframe src="/gradio" width="100%" height="
|
106 |
</body>
|
107 |
</html>
|
108 |
"""
|
109 |
|
110 |
-
# Mount Gradio
|
111 |
app.mount("/gradio", gradio_ui.app)
|
112 |
app.mount("/static", StaticFiles(directory="static"), name="static")
|
113 |
|
114 |
-
# For Hugging Face Spaces
|
115 |
def get_app():
|
116 |
-
return app
|
|
|
1 |
from fastapi import FastAPI
|
2 |
from fastapi.middleware.cors import CORSMiddleware
|
|
|
3 |
from fastapi.responses import HTMLResponse
|
4 |
+
from fastapi.staticfiles import StaticFiles
|
5 |
import gradio as gr
|
6 |
import numpy as np
|
7 |
import faiss
|
8 |
import logging
|
9 |
+
import os
|
10 |
import requests
|
11 |
import json
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
from sentence_transformers import SentenceTransformer
|
|
|
|
|
13 |
|
14 |
+
# === Environment variables (safe cache paths for HF Spaces) ===
|
15 |
+
os.environ["TRANSFORMERS_CACHE"] = "/data/transformers"
|
16 |
+
os.environ["HF_HOME"] = "/data/hf"
|
17 |
+
os.environ["SENTENCE_TRANSFORMERS_HOME"] = "/data/st"
|
|
|
18 |
|
19 |
+
# === Load embedding model ===
|
20 |
+
model = SentenceTransformer("sentence-transformers/all-MiniLM-L6-v2")
|
|
|
21 |
index = faiss.IndexFlatL2(384)
|
22 |
memory_text = []
|
23 |
|
24 |
+
# === Fireworks API ===
|
25 |
+
FIREWORKS_API_KEY = os.getenv("FIREWORKS_API_KEY") # 🔐 Use env var instead of hardcoding
|
26 |
+
FIREWORKS_URL = "https://api.fireworks.ai/inference/v1/chat/completions"
|
27 |
+
|
28 |
+
def query_fireworks(prompt):
|
29 |
+
payload = {
|
30 |
+
"model": "accounts/fireworks/models/deepseek-r1",
|
31 |
+
"max_tokens": 4096,
|
32 |
+
"top_p": 1,
|
33 |
+
"top_k": 40,
|
34 |
+
"temperature": 0.6,
|
35 |
+
"messages": [{"role": "user", "content": prompt}],
|
36 |
+
}
|
37 |
+
headers = {
|
38 |
+
"Accept": "application/json",
|
39 |
+
"Content-Type": "application/json",
|
40 |
+
"Authorization": f"Bearer {FIREWORKS_API_KEY}"
|
41 |
+
}
|
42 |
+
|
43 |
+
response = requests.post(FIREWORKS_URL, headers=headers, data=json.dumps(payload))
|
44 |
+
result = response.json()
|
45 |
+
return result.get("choices", [{}])[0].get("message", {}).get("content", "⚠️ No response.")
|
46 |
+
|
47 |
+
# === Autonomous Agent ===
|
48 |
def autonomous_agent(input_text):
|
49 |
+
vec = model.encode([input_text])[0]
|
50 |
response = ""
|
51 |
|
52 |
if index.ntotal > 0:
|
53 |
+
D, I = index.search(np.array([vec]), min(5, index.ntotal))
|
54 |
+
matches = [memory_text[idx] for idx, dist in zip(I[0], D[0]) if idx != -1 and dist < 0.8]
|
|
|
|
|
|
|
|
|
|
|
55 |
if matches:
|
56 |
+
response += "🧠 Related memories:\n- " + "\n- ".join(matches[:3]) + "\n\n"
|
|
|
|
|
|
|
|
|
57 |
|
58 |
+
# Store current memory
|
59 |
index.add(np.array([vec]))
|
60 |
memory_text.append(input_text)
|
61 |
|
62 |
+
# 🔥 Query LLM (Fireworks)
|
63 |
+
llm_response = query_fireworks(input_text)
|
64 |
+
response += f"🤖 Response:\n{llm_response}"
|
65 |
return response
|
66 |
|
67 |
+
# === Gradio UI ===
|
68 |
gradio_ui = gr.Interface(
|
69 |
fn=autonomous_agent,
|
70 |
inputs="text",
|
71 |
outputs="text",
|
72 |
title="Autonomous AI Agent",
|
73 |
+
description="Self-enhancing chatbot with memory + Fireworks LLM",
|
74 |
+
)
|
75 |
+
|
76 |
+
# === FastAPI App ===
|
77 |
+
app = FastAPI()
|
78 |
+
|
79 |
+
app.add_middleware(
|
80 |
+
CORSMiddleware,
|
81 |
+
allow_origins=["*"], allow_methods=["*"], allow_headers=["*"],
|
82 |
)
|
83 |
|
84 |
+
# === Root Web UI with embedded Gradio ===
|
85 |
@app.get("/", response_class=HTMLResponse)
|
86 |
async def root():
|
87 |
return """
|
88 |
<html>
|
89 |
+
<head><title>Autonomous AI Agent</title></head>
|
|
|
|
|
90 |
<body>
|
91 |
+
<h2>Autonomous AI Agent</h2>
|
92 |
+
<iframe src="/gradio" width="100%" height="600"></iframe>
|
93 |
</body>
|
94 |
</html>
|
95 |
"""
|
96 |
|
97 |
+
# === Mount Gradio & static files ===
|
98 |
app.mount("/gradio", gradio_ui.app)
|
99 |
app.mount("/static", StaticFiles(directory="static"), name="static")
|
100 |
|
101 |
+
# === For Hugging Face Spaces ===
|
102 |
def get_app():
|
103 |
+
return app
|