Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -11,6 +11,14 @@ import re
|
|
| 11 |
import torch
|
| 12 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
app = Flask(__name__)
|
| 15 |
|
| 16 |
# ---------- Firebase ----------
|
|
@@ -28,22 +36,35 @@ print("✅ Firebase connected")
|
|
| 28 |
embedder = SentenceTransformer("sentence-transformers/all-MiniLM-L6-v2")
|
| 29 |
print("✅ Embedding model loaded")
|
| 30 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
# ---------- Load GPT‑2 (final fallback) ----------
|
| 32 |
gpt_model = None
|
| 33 |
gpt_tokenizer = None
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
|
|
|
| 45 |
def call_ai(prompt):
|
| 46 |
-
# 1. Groq (
|
| 47 |
groq_key = os.getenv("GROQ_API_KEY")
|
| 48 |
if groq_key:
|
| 49 |
url = "https://api.groq.com/openai/v1/chat/completions"
|
|
@@ -61,8 +82,28 @@ def call_ai(prompt):
|
|
| 61 |
except Exception as e:
|
| 62 |
print(f"Groq error: {e}")
|
| 63 |
|
| 64 |
-
# 2. OpenRouter
|
| 65 |
openrouter_key = os.getenv("OPENROUTER_API_KEY")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
if openrouter_key:
|
| 67 |
url = "https://openrouter.ai/api/v1/chat/completions"
|
| 68 |
headers = {"Authorization": f"Bearer {openrouter_key}", "Content-Type": "application/json"}
|
|
@@ -77,9 +118,21 @@ def call_ai(prompt):
|
|
| 77 |
if resp.status_code == 200:
|
| 78 |
return resp.json()["choices"][0]["message"]["content"].strip()
|
| 79 |
except Exception as e:
|
| 80 |
-
print(f"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
|
| 82 |
-
#
|
| 83 |
if gpt_model is not None:
|
| 84 |
try:
|
| 85 |
inputs = gpt_tokenizer(prompt, return_tensors="pt", truncation=True, max_length=1024)
|
|
@@ -100,7 +153,7 @@ def call_ai(prompt):
|
|
| 100 |
except Exception as e:
|
| 101 |
print(f"GPT‑2 error: {e}")
|
| 102 |
|
| 103 |
-
#
|
| 104 |
return "What else would you like to share about your project?"
|
| 105 |
|
| 106 |
# ---------- Helper: Load extra services from Firebase ----------
|
|
@@ -322,10 +375,8 @@ def process_step_by_step(session_id, message):
|
|
| 322 |
elif step == "asking_extra":
|
| 323 |
extra_services = get_extra_services()
|
| 324 |
if msg_lower in ["none", "done", "no"]:
|
| 325 |
-
# Move to deep questions (no AI used for decision)
|
| 326 |
session["step"] = "asking_deep"
|
| 327 |
session["deep_index"] = 0
|
| 328 |
-
# Generate first deep question using AI
|
| 329 |
prompt = f"""You are an expert project estimator. Based on the following client requirements, ask the NEXT most important, specific, and open‑ended question to clarify the project details. Do not repeat what has already been answered. Ask about design, content, audience, integrations, or any missing critical detail. Keep your question concise (one sentence).
|
| 330 |
|
| 331 |
Current requirements:
|
|
@@ -333,7 +384,6 @@ Current requirements:
|
|
| 333 |
|
| 334 |
Generated question:"""
|
| 335 |
first_q = call_ai(prompt)
|
| 336 |
-
# Sanitize AI response: if too long or contains braces (likely code), use a default
|
| 337 |
if len(first_q) > 300 or '{' in first_q or '}' in first_q or '```' in first_q:
|
| 338 |
first_q = "Could you please provide more details about your target audience or business goals?"
|
| 339 |
session["deep_questions"] = [first_q]
|
|
@@ -353,6 +403,7 @@ Generated question:"""
|
|
| 353 |
extra_list.append(matched_key)
|
| 354 |
req["extra_features"] = extra_list
|
| 355 |
session["requirements"] = req
|
|
|
|
| 356 |
return f"✅ Added **{extra_services[matched_key]['name']}**.\n\nAny other extras? Type name or **done**."
|
| 357 |
else:
|
| 358 |
return f"ℹ️ Already added. Type another name or **done**."
|
|
|
|
| 11 |
import torch
|
| 12 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 13 |
|
| 14 |
+
# Try to load llama-cpp-python (GGUF support)
|
| 15 |
+
try:
|
| 16 |
+
from llama_cpp import Llama
|
| 17 |
+
LLAMA_AVAILABLE = True
|
| 18 |
+
except ImportError:
|
| 19 |
+
LLAMA_AVAILABLE = False
|
| 20 |
+
print("⚠️ llama-cpp-python not installed. Llama fallback unavailable.")
|
| 21 |
+
|
| 22 |
app = Flask(__name__)
|
| 23 |
|
| 24 |
# ---------- Firebase ----------
|
|
|
|
| 36 |
embedder = SentenceTransformer("sentence-transformers/all-MiniLM-L6-v2")
|
| 37 |
print("✅ Embedding model loaded")
|
| 38 |
|
| 39 |
+
# ---------- Load Llama 3.2 1B GGUF (if available) ----------
|
| 40 |
+
llama_model = None
|
| 41 |
+
llama_path = os.getenv("LLAMA_GGUF_PATH")
|
| 42 |
+
if LLAMA_AVAILABLE and llama_path and os.path.exists(llama_path):
|
| 43 |
+
try:
|
| 44 |
+
llama_model = Llama(model_path=llama_path, n_ctx=2048, n_threads=2, verbose=False)
|
| 45 |
+
print("✅ Llama 3.2 1B GGUF loaded (fallback)")
|
| 46 |
+
except Exception as e:
|
| 47 |
+
print(f"⚠️ Failed to load Llama GGUF: {e}")
|
| 48 |
+
else:
|
| 49 |
+
print("⚠️ Llama GGUF not configured – will use GPT‑2 fallback if available")
|
| 50 |
+
|
| 51 |
# ---------- Load GPT‑2 (final fallback) ----------
|
| 52 |
gpt_model = None
|
| 53 |
gpt_tokenizer = None
|
| 54 |
+
if not llama_model:
|
| 55 |
+
try:
|
| 56 |
+
gpt_tokenizer = AutoTokenizer.from_pretrained("gpt2")
|
| 57 |
+
gpt_model = AutoModelForCausalLM.from_pretrained("gpt2")
|
| 58 |
+
gpt_tokenizer.pad_token = gpt_tokenizer.eos_token
|
| 59 |
+
if torch.cuda.is_available():
|
| 60 |
+
gpt_model = gpt_model.to("cuda")
|
| 61 |
+
print("✅ GPT‑2 loaded (final fallback)")
|
| 62 |
+
except Exception as e:
|
| 63 |
+
print(f"⚠️ GPT‑2 load failed: {e}")
|
| 64 |
+
|
| 65 |
+
# ---------- AI Call (Groq → Owl Alpha → Gemini → Llama GGUF → GPT‑2 → rule) ----------
|
| 66 |
def call_ai(prompt):
|
| 67 |
+
# 1. Groq (fast, free)
|
| 68 |
groq_key = os.getenv("GROQ_API_KEY")
|
| 69 |
if groq_key:
|
| 70 |
url = "https://api.groq.com/openai/v1/chat/completions"
|
|
|
|
| 82 |
except Exception as e:
|
| 83 |
print(f"Groq error: {e}")
|
| 84 |
|
| 85 |
+
# 2. OpenRouter Owl Alpha (primary – free, 1M context)
|
| 86 |
openrouter_key = os.getenv("OPENROUTER_API_KEY")
|
| 87 |
+
if openrouter_key:
|
| 88 |
+
url = "https://openrouter.ai/api/v1/chat/completions"
|
| 89 |
+
headers = {
|
| 90 |
+
"Authorization": f"Bearer {openrouter_key}",
|
| 91 |
+
"Content-Type": "application/json"
|
| 92 |
+
}
|
| 93 |
+
payload = {
|
| 94 |
+
"model": "openrouter/owl-alpha",
|
| 95 |
+
"messages": [{"role": "user", "content": prompt}],
|
| 96 |
+
"temperature": 0.7,
|
| 97 |
+
"max_tokens": 150
|
| 98 |
+
}
|
| 99 |
+
try:
|
| 100 |
+
resp = requests.post(url, json=payload, headers=headers, timeout=10)
|
| 101 |
+
if resp.status_code == 200:
|
| 102 |
+
return resp.json()["choices"][0]["message"]["content"].strip()
|
| 103 |
+
except Exception as e:
|
| 104 |
+
print(f"Owl Alpha error: {e}")
|
| 105 |
+
|
| 106 |
+
# 3. OpenRouter Gemini (fallback)
|
| 107 |
if openrouter_key:
|
| 108 |
url = "https://openrouter.ai/api/v1/chat/completions"
|
| 109 |
headers = {"Authorization": f"Bearer {openrouter_key}", "Content-Type": "application/json"}
|
|
|
|
| 118 |
if resp.status_code == 200:
|
| 119 |
return resp.json()["choices"][0]["message"]["content"].strip()
|
| 120 |
except Exception as e:
|
| 121 |
+
print(f"Gemini error: {e}")
|
| 122 |
+
|
| 123 |
+
# 4. Llama 3.2 1B GGUF (local)
|
| 124 |
+
if llama_model:
|
| 125 |
+
try:
|
| 126 |
+
output = llama_model.create_chat_completion(
|
| 127 |
+
messages=[{"role": "user", "content": prompt}],
|
| 128 |
+
temperature=0.7,
|
| 129 |
+
max_tokens=150
|
| 130 |
+
)
|
| 131 |
+
return output["choices"][0]["message"]["content"].strip()
|
| 132 |
+
except Exception as e:
|
| 133 |
+
print(f"Llama GGUF error: {e}")
|
| 134 |
|
| 135 |
+
# 5. GPT‑2 fallback
|
| 136 |
if gpt_model is not None:
|
| 137 |
try:
|
| 138 |
inputs = gpt_tokenizer(prompt, return_tensors="pt", truncation=True, max_length=1024)
|
|
|
|
| 153 |
except Exception as e:
|
| 154 |
print(f"GPT‑2 error: {e}")
|
| 155 |
|
| 156 |
+
# 6. Ultimate rule‑based
|
| 157 |
return "What else would you like to share about your project?"
|
| 158 |
|
| 159 |
# ---------- Helper: Load extra services from Firebase ----------
|
|
|
|
| 375 |
elif step == "asking_extra":
|
| 376 |
extra_services = get_extra_services()
|
| 377 |
if msg_lower in ["none", "done", "no"]:
|
|
|
|
| 378 |
session["step"] = "asking_deep"
|
| 379 |
session["deep_index"] = 0
|
|
|
|
| 380 |
prompt = f"""You are an expert project estimator. Based on the following client requirements, ask the NEXT most important, specific, and open‑ended question to clarify the project details. Do not repeat what has already been answered. Ask about design, content, audience, integrations, or any missing critical detail. Keep your question concise (one sentence).
|
| 381 |
|
| 382 |
Current requirements:
|
|
|
|
| 384 |
|
| 385 |
Generated question:"""
|
| 386 |
first_q = call_ai(prompt)
|
|
|
|
| 387 |
if len(first_q) > 300 or '{' in first_q or '}' in first_q or '```' in first_q:
|
| 388 |
first_q = "Could you please provide more details about your target audience or business goals?"
|
| 389 |
session["deep_questions"] = [first_q]
|
|
|
|
| 403 |
extra_list.append(matched_key)
|
| 404 |
req["extra_features"] = extra_list
|
| 405 |
session["requirements"] = req
|
| 406 |
+
# This triggers invoice add in the frontend
|
| 407 |
return f"✅ Added **{extra_services[matched_key]['name']}**.\n\nAny other extras? Type name or **done**."
|
| 408 |
else:
|
| 409 |
return f"ℹ️ Already added. Type another name or **done**."
|