Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,35 +1,31 @@
|
|
1 |
from fastapi import FastAPI, HTTPException, Request
|
2 |
-
from fastapi.responses import StreamingResponse
|
3 |
from fastapi.middleware.cors import CORSMiddleware
|
4 |
import aiohttp
|
5 |
import json
|
6 |
-
import time
|
7 |
-
import random
|
8 |
import ast
|
9 |
-
import urllib.parse
|
10 |
-
from apscheduler.schedulers.background import BackgroundScheduler
|
11 |
import os
|
12 |
from pydantic import BaseModel
|
|
|
|
|
|
|
13 |
|
14 |
SAMBA_NOVA_API_KEY = os.environ.get("SAMBA_NOVA_API_KEY", None)
|
15 |
|
16 |
app = FastAPI()
|
17 |
|
18 |
-
#
|
19 |
-
|
20 |
-
CACHE_DURATION = 120
|
21 |
|
22 |
-
#
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
scheduler.add_job(cleanup_cache, 'interval', seconds=60) # Run cleanup every 60 seconds
|
32 |
-
scheduler.start()
|
33 |
|
34 |
class StreamTextRequest(BaseModel):
|
35 |
query: str
|
@@ -38,43 +34,31 @@ class StreamTextRequest(BaseModel):
|
|
38 |
api_key: str = None
|
39 |
|
40 |
@app.post("/stream_text")
|
|
|
41 |
async def stream_text(request: StreamTextRequest):
|
42 |
-
current_time = time.time()
|
43 |
-
cache_key = (request.query, request.history, request.model)
|
44 |
-
|
45 |
-
# Check if the request is in the cache and not expired
|
46 |
-
if cache_key in cache:
|
47 |
-
cached_response, timestamp = cache[cache_key]
|
48 |
-
return StreamingResponse(iter([f"{cached_response}"]), media_type='text/event-stream')
|
49 |
-
|
50 |
# Model selection logic
|
51 |
if "405" in request.model:
|
52 |
fmodel = "Meta-Llama-3.1-405B-Instruct"
|
53 |
-
|
54 |
-
fmodel = "Meta-Llama-3.
|
55 |
else:
|
56 |
-
fmodel = "Meta-Llama-3.1-
|
57 |
|
58 |
system_message = """You are Voicee, a friendly and intelligent voice assistant created by KingNish. Your primary goal is to provide accurate, concise, and engaging responses while maintaining a positive and upbeat tone. Always aim to provide clear and relevant information that directly addresses the user's query, but feel free to sprinkle in a dash of humor—after all, laughter is the best app! Keep your responses brief and to the point, avoiding unnecessary details or tangents, unless they’re hilariously relevant. Use a friendly and approachable tone to create a pleasant interaction, and don’t shy away from a cheeky pun or two! Tailor your responses based on the user's input and previous interactions, ensuring a personalized experience that feels like chatting with a witty friend. Invite users to ask follow-up questions or clarify their needs, fostering a conversational flow that’s as smooth as butter on a hot pancake. Aim to put a smile on the user's face with light-hearted and fun responses, and be proactive in offering additional help or suggestions related to the user's query. Remember, your goal is to be the go-to assistant for users, making their experience enjoyable and informative—like a delightful dessert after a hearty meal!"""
|
59 |
|
60 |
messages = [{'role': 'system', 'content': system_message}]
|
61 |
-
|
62 |
messages.extend(ast.literal_eval(request.history))
|
63 |
-
|
64 |
messages.append({'role': 'user', 'content': request.query})
|
65 |
|
66 |
data = {'messages': messages, 'stream': True, 'model': fmodel}
|
67 |
-
|
68 |
api_key = request.api_key if request.api_key != 'none' else SAMBA_NOVA_API_KEY
|
69 |
|
70 |
-
|
71 |
async def stream_response():
|
72 |
async with aiohttp.ClientSession() as session:
|
73 |
-
async with session.post('https://api.sambanova.ai/v1/chat/completions', headers
|
74 |
if response.status != 200:
|
75 |
raise HTTPException(status_code=response.status, detail="Error fetching AI response")
|
76 |
|
77 |
-
response_content = ""
|
78 |
async for line in response.content:
|
79 |
line = line.decode('utf-8').strip()
|
80 |
if line.startswith('data: {'):
|
@@ -84,22 +68,13 @@ async def stream_text(request: StreamTextRequest):
|
|
84 |
content = parsed_data.get("choices", [{}])[0].get("delta", {}).get("content", '')
|
85 |
if content:
|
86 |
content = content.replace("\n", " ")
|
87 |
-
response_content += f"data: {content}\n\n"
|
88 |
yield f"data: {content}\n\n"
|
89 |
except json.JSONDecodeError as e:
|
90 |
print(f"Error decoding JSON: {e}")
|
91 |
yield f"data: Error decoding JSON\n\n"
|
92 |
|
93 |
-
# Cache the full response
|
94 |
-
cache[cache_key] = (response_content, current_time)
|
95 |
-
|
96 |
return StreamingResponse(stream_response(), media_type='text/event-stream')
|
97 |
|
98 |
-
|
99 |
-
|
100 |
-
# Serve index.html from the same directory as your main.py file
|
101 |
-
from starlette.responses import FileResponse
|
102 |
-
|
103 |
@app.get("/script1.js")
|
104 |
async def script1_js():
|
105 |
return FileResponse("script1.js")
|
|
|
1 |
from fastapi import FastAPI, HTTPException, Request
|
2 |
+
from fastapi.responses import StreamingResponse, FileResponse
|
3 |
from fastapi.middleware.cors import CORSMiddleware
|
4 |
import aiohttp
|
5 |
import json
|
|
|
|
|
6 |
import ast
|
|
|
|
|
7 |
import os
|
8 |
from pydantic import BaseModel
|
9 |
+
from datetime import datetime, timedelta
|
10 |
+
from aiocache import cached, SimpleMemoryCache
|
11 |
+
from aiocache.serializers import JsonSerializer
|
12 |
|
13 |
SAMBA_NOVA_API_KEY = os.environ.get("SAMBA_NOVA_API_KEY", None)
|
14 |
|
15 |
app = FastAPI()
|
16 |
|
17 |
+
# Constants for Caching
|
18 |
+
CACHE_TIME_SECONDS = 24 * 3600 # 24 hours in seconds
|
|
|
19 |
|
20 |
+
# Cache Middleware to add caching headers
|
21 |
+
@app.middleware("http")
|
22 |
+
async def add_cache_headers(request: Request, call_next):
|
23 |
+
response = await call_next(request)
|
24 |
+
if response.status_code == 200 and request.url.path != "/" and request.url.path != "/script1.js" and request.url.path != "/script2.js" and request.url.path != "/styles.css":
|
25 |
+
expires = datetime.utcnow() + timedelta(seconds=CACHE_TIME_SECONDS)
|
26 |
+
response.headers["Cache-Control"] = f"public, max-age={CACHE_TIME_SECONDS}"
|
27 |
+
response.headers["Expires"] = expires.strftime("%a, %d %b %Y %H:%M:%S GMT")
|
28 |
+
return response
|
|
|
|
|
29 |
|
30 |
class StreamTextRequest(BaseModel):
|
31 |
query: str
|
|
|
34 |
api_key: str = None
|
35 |
|
36 |
@app.post("/stream_text")
|
37 |
+
@cached(ttl=600, cache=SimpleMemoryCache, serializer=JsonSerializer())
|
38 |
async def stream_text(request: StreamTextRequest):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
39 |
# Model selection logic
|
40 |
if "405" in request.model:
|
41 |
fmodel = "Meta-Llama-3.1-405B-Instruct"
|
42 |
+
elif "70" in request.model:
|
43 |
+
fmodel = "Meta-Llama-3.3-70B-Instruct"
|
44 |
else:
|
45 |
+
fmodel = "Meta-Llama-3.1-1B-Instruct"
|
46 |
|
47 |
system_message = """You are Voicee, a friendly and intelligent voice assistant created by KingNish. Your primary goal is to provide accurate, concise, and engaging responses while maintaining a positive and upbeat tone. Always aim to provide clear and relevant information that directly addresses the user's query, but feel free to sprinkle in a dash of humor—after all, laughter is the best app! Keep your responses brief and to the point, avoiding unnecessary details or tangents, unless they’re hilariously relevant. Use a friendly and approachable tone to create a pleasant interaction, and don’t shy away from a cheeky pun or two! Tailor your responses based on the user's input and previous interactions, ensuring a personalized experience that feels like chatting with a witty friend. Invite users to ask follow-up questions or clarify their needs, fostering a conversational flow that’s as smooth as butter on a hot pancake. Aim to put a smile on the user's face with light-hearted and fun responses, and be proactive in offering additional help or suggestions related to the user's query. Remember, your goal is to be the go-to assistant for users, making their experience enjoyable and informative—like a delightful dessert after a hearty meal!"""
|
48 |
|
49 |
messages = [{'role': 'system', 'content': system_message}]
|
|
|
50 |
messages.extend(ast.literal_eval(request.history))
|
|
|
51 |
messages.append({'role': 'user', 'content': request.query})
|
52 |
|
53 |
data = {'messages': messages, 'stream': True, 'model': fmodel}
|
|
|
54 |
api_key = request.api_key if request.api_key != 'none' else SAMBA_NOVA_API_KEY
|
55 |
|
|
|
56 |
async def stream_response():
|
57 |
async with aiohttp.ClientSession() as session:
|
58 |
+
async with session.post('https://api.sambanova.ai/v1/chat/completions', headers={'Authorization': f'Bearer {api_key}', 'Content-Type': 'application/json'}, json=data) as response:
|
59 |
if response.status != 200:
|
60 |
raise HTTPException(status_code=response.status, detail="Error fetching AI response")
|
61 |
|
|
|
62 |
async for line in response.content:
|
63 |
line = line.decode('utf-8').strip()
|
64 |
if line.startswith('data: {'):
|
|
|
68 |
content = parsed_data.get("choices", [{}])[0].get("delta", {}).get("content", '')
|
69 |
if content:
|
70 |
content = content.replace("\n", " ")
|
|
|
71 |
yield f"data: {content}\n\n"
|
72 |
except json.JSONDecodeError as e:
|
73 |
print(f"Error decoding JSON: {e}")
|
74 |
yield f"data: Error decoding JSON\n\n"
|
75 |
|
|
|
|
|
|
|
76 |
return StreamingResponse(stream_response(), media_type='text/event-stream')
|
77 |
|
|
|
|
|
|
|
|
|
|
|
78 |
@app.get("/script1.js")
|
79 |
async def script1_js():
|
80 |
return FileResponse("script1.js")
|