File size: 2,338 Bytes
d7f16fb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import os
from fastapi import FastAPI
from pydantic import BaseModel
from huggingface_hub import InferenceClient
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import StreamingResponse

app = FastAPI()

app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

# Connect to the stable Qwen-7B Brain (100% Free on HF Cloud)
client = InferenceClient(
    model="Qwen/Qwen2.5-7B-Instruct",
    token=os.getenv("HF_TOKEN")
)

memory_store = {}

# CUSTOMIZABLE IDENTITY: Your friend can edit this text!
AI_IDENTITY = """
You are a highly advanced, warm, and supportive AI companion.
Your name is ZENVY AI
You were built by Rik Sarkar
Always be encouraging, speak naturally, and help the user.
"""

class ChatRequest(BaseModel):
    message: str
    session_id: str
  
@app.get("/")
def home():
    return {"status": "ZENVY V1 is Online"}

@app.post("/chat")
async def chat(request: ChatRequest):
    session_id = request.session_id
    if session_id not in memory_store:
        memory_store[session_id] = []
        
    history = memory_store[session_id]
    history.append({"role": "user", "content": request.message})

    def event_generator():
        collected_reply = []
        try:
            messages = [{"role": "system", "content": AI_IDENTITY}] + history
            
            response = client.chat_completion(
                messages=messages,
                max_tokens=4096,  # Raised output token capacity for long, complete responses
                temperature=0.7,
                stream=True
            )
            
            for chunk in response:
                if chunk.choices and len(chunk.choices) > 0:
                    word = chunk.choices[0].delta.content
                    if word:
                        collected_reply.append(word)
                        yield word
            
            full_reply = "".join(collected_reply)
            history.append({"role": "assistant", "content": full_reply})
            
            if len(history) > 12:
                history.pop(0)
                history.pop(0)
                
        except Exception as e:
            yield f"Error: {str(e)}"

    return StreamingResponse(event_generator(), media_type="text/event-stream")