Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,11 +1,10 @@
|
|
| 1 |
from fastapi import FastAPI, Request
|
| 2 |
import requests
|
| 3 |
-
|
|
|
|
| 4 |
from datetime import datetime
|
| 5 |
from fastapi.middleware.cors import CORSMiddleware
|
| 6 |
import uvicorn
|
| 7 |
-
import os
|
| 8 |
-
import time
|
| 9 |
|
| 10 |
app = FastAPI()
|
| 11 |
|
|
@@ -16,17 +15,13 @@ app.add_middleware(
|
|
| 16 |
allow_headers=["*"],
|
| 17 |
)
|
| 18 |
|
| 19 |
-
# 🔱 API Configuration
|
| 20 |
-
# මෙය Hugging Face වල නොමිලේ දෙන API එකකි
|
| 21 |
API_URL = "https://api-inference.huggingface.co/models/deepseek-ai/DeepSeek-R1-Distill-Qwen-1.5B"
|
| 22 |
-
# ඔබට Hugging Face Token එකක් තිබේ නම් මෙහි ඇතුළත් කළ හැක, නැතත් වැඩ කරයි (සීමිතව)
|
| 23 |
-
HEADERS = {"Authorization": "Bearer YOUR_HF_TOKEN_OPTIONAL"}
|
| 24 |
|
| 25 |
def web_search(query):
|
| 26 |
try:
|
| 27 |
with DDGS() as ddgs:
|
| 28 |
results = [r for r in ddgs.text(query, max_results=3)]
|
| 29 |
-
return "\n".join([f"
|
| 30 |
except: return ""
|
| 31 |
|
| 32 |
@app.post("/generate")
|
|
@@ -34,32 +29,33 @@ async def generate(request: Request):
|
|
| 34 |
try:
|
| 35 |
data = await request.json()
|
| 36 |
user_prompt = data.get("prompt")
|
|
|
|
| 37 |
current_date = datetime.now().strftime("%Y-%m-%d")
|
| 38 |
-
search_data = web_search(user_prompt)
|
| 39 |
|
| 40 |
-
# 🔱 Reasoning Prompt
|
| 41 |
full_prompt = (
|
| 42 |
-
f"<|system|>\nToday is {current_date}.
|
| 43 |
f"<|user|>\n{user_prompt}\n<|assistant|>\n<think>\n"
|
| 44 |
)
|
| 45 |
|
| 46 |
-
payload = {
|
| 47 |
-
"inputs": full_prompt,
|
| 48 |
-
"parameters": {"max_new_tokens": 1024, "temperature": 0.1}
|
| 49 |
-
}
|
| 50 |
-
|
| 51 |
-
response = requests.post(API_URL, headers=HEADERS, json=payload)
|
| 52 |
-
output = response.json()
|
| 53 |
|
| 54 |
-
#
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
-
return {"response":
|
| 63 |
|
| 64 |
except Exception as e:
|
| 65 |
return {"error": str(e)}
|
|
|
|
| 1 |
from fastapi import FastAPI, Request
|
| 2 |
import requests
|
| 3 |
+
import time
|
| 4 |
+
from ddgs import DDGS
|
| 5 |
from datetime import datetime
|
| 6 |
from fastapi.middleware.cors import CORSMiddleware
|
| 7 |
import uvicorn
|
|
|
|
|
|
|
| 8 |
|
| 9 |
app = FastAPI()
|
| 10 |
|
|
|
|
| 15 |
allow_headers=["*"],
|
| 16 |
)
|
| 17 |
|
|
|
|
|
|
|
| 18 |
API_URL = "https://api-inference.huggingface.co/models/deepseek-ai/DeepSeek-R1-Distill-Qwen-1.5B"
|
|
|
|
|
|
|
| 19 |
|
| 20 |
def web_search(query):
|
| 21 |
try:
|
| 22 |
with DDGS() as ddgs:
|
| 23 |
results = [r for r in ddgs.text(query, max_results=3)]
|
| 24 |
+
return "\n".join([f"Web Info: {r['body']}" for r in results])
|
| 25 |
except: return ""
|
| 26 |
|
| 27 |
@app.post("/generate")
|
|
|
|
| 29 |
try:
|
| 30 |
data = await request.json()
|
| 31 |
user_prompt = data.get("prompt")
|
| 32 |
+
search_results = web_search(user_prompt)
|
| 33 |
current_date = datetime.now().strftime("%Y-%m-%d")
|
|
|
|
| 34 |
|
|
|
|
| 35 |
full_prompt = (
|
| 36 |
+
f"<|system|>\nToday is {current_date}. Context: {search_results}\n"
|
| 37 |
f"<|user|>\n{user_prompt}\n<|assistant|>\n<think>\n"
|
| 38 |
)
|
| 39 |
|
| 40 |
+
payload = {"inputs": full_prompt, "parameters": {"max_new_tokens": 1024, "temperature": 0.1, "return_full_text": False}}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
+
# 🔱 Retry Logic: API එක සූදානම් වන තෙක් වාර 5ක් උත්සාහ කරයි
|
| 43 |
+
for attempt in range(5):
|
| 44 |
+
response = requests.post(API_URL, json=payload)
|
| 45 |
+
output = response.json()
|
| 46 |
+
|
| 47 |
+
if isinstance(output, list) and 'generated_text' in output[0]:
|
| 48 |
+
final_answer = output[0]['generated_text'].split("</think>")[-1].strip()
|
| 49 |
+
return {"response": final_answer}
|
| 50 |
+
|
| 51 |
+
# මොඩලය Load වෙමින් පවතී නම් තත්පර 5ක් රැඳී සිට නැවත උත්සාහ කරයි
|
| 52 |
+
if "estimated_time" in str(output):
|
| 53 |
+
print(f"🔱 Engine Loading... Attempt {attempt+1}")
|
| 54 |
+
time.sleep(5)
|
| 55 |
+
else:
|
| 56 |
+
break
|
| 57 |
|
| 58 |
+
return {"response": "System is calibrating. Please send the command again in 10 seconds."}
|
| 59 |
|
| 60 |
except Exception as e:
|
| 61 |
return {"error": str(e)}
|