DarkMindForever commited on
Commit
7bd5dc1
·
verified ·
1 Parent(s): f66c18b

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +63 -0
app.py ADDED
@@ -0,0 +1,63 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import subprocess
2
+ import time
3
+ import os
4
+ import requests
5
+ from fastapi import FastAPI, Request
6
+ from fastapi.responses import StreamingResponse
7
+ from duckduckgo_search import DDGS
8
+
9
+ app = FastAPI()
10
+
11
+ # 1. Boot the AI Engine in the background
12
+ subprocess.Popen([
13
+ "/app/llama-server",
14
+ "--model", "/models/model.gguf",
15
+ "--host", "127.0.0.1",
16
+ "--port", "8080",
17
+ "--nopreload"
18
+ ])
19
+
20
+ def fetch_live_data(query):
21
+ """Fetches real-time web info for news, scores, and dates"""
22
+ try:
23
+ with DDGS() as ddgs:
24
+ results = [f"{r['title']}: {r['body']}" for r in ddgs.text(query, max_results=3)]
25
+ return "\n".join(results)
26
+ except Exception:
27
+ return "No live data found, using internal 2026 knowledge."
28
+
29
+ @app.post("/v1/chat/completions")
30
+ async def chat_proxy(request: Request):
31
+ body = await request.json()
32
+ user_query = body["messages"][-1]["content"]
33
+
34
+ # Detect if user needs live info
35
+ live_triggers = ["news", "score", "price", "weather", "today", "who won"]
36
+ context = ""
37
+ if any(word in user_query.lower() for word in live_triggers):
38
+ context = f"\n[LIVE 2026 DATA]: {fetch_live_data(user_query)}"
39
+
40
+ # Inject the "Positive Power" personality and live context
41
+ system_instr = (
42
+ "You are a high-speed, witty AI with Positive Power. "
43
+ "Use the provided live data to give accurate news/scores. "
44
+ "Be extremely brief (max 3 sentences) and highly motivational! "
45
+ "Current Date: Feb 2026."
46
+ )
47
+
48
+ body["messages"].insert(0, {"role": "system", "content": system_instr})
49
+ body["messages"][-1]["content"] += context
50
+
51
+ # Forward to local llama-server
52
+ def stream_response():
53
+ with requests.post("http://127.0.0.1:8080/v1/chat/completions", json=body, stream=True) as r:
54
+ for chunk in r.iter_content(chunk_size=128):
55
+ yield chunk
56
+
57
+ return StreamingResponse(stream_response(), media_type="application/json")
58
+
59
+ if __name__ == "__main__":
60
+ # Wait for the model to load into memory
61
+ time.sleep(10)
62
+ import uvicorn
63
+ uvicorn.run(app, host="0.0.0.0", port=7860)