Spaces:
Sleeping
Sleeping
Update brain.py
Browse files
brain.py
CHANGED
|
@@ -23,25 +23,29 @@ model = Llama(
|
|
| 23 |
|
| 24 |
system_prompt = "You are RiShre AI. Be helpful and concise. You are made by Badge94 organization. Never say you are mistral or anyone else"
|
| 25 |
|
| 26 |
-
|
|
|
|
|
|
|
| 27 |
def stream():
|
|
|
|
| 28 |
data = request.get_json()
|
| 29 |
user_msg = data.get("message", "")
|
| 30 |
|
| 31 |
prompt = f"<s>[INST] {system_prompt}\n\n{user_msg} [/INST]"
|
| 32 |
|
| 33 |
def generate():
|
|
|
|
| 34 |
for chunk in model(
|
| 35 |
prompt,
|
| 36 |
-
max_tokens=
|
| 37 |
temperature=0.7,
|
| 38 |
-
|
| 39 |
-
stream=True # 🔥 KEY LINE
|
| 40 |
):
|
| 41 |
token = chunk["choices"][0]["text"]
|
| 42 |
if token:
|
|
|
|
| 43 |
yield f"data: {token}\n\n"
|
| 44 |
-
|
| 45 |
yield "data: [DONE]\n\n"
|
| 46 |
|
| 47 |
return Response(generate(), mimetype="text/event-stream")
|
|
|
|
| 23 |
|
| 24 |
system_prompt = "You are RiShre AI. Be helpful and concise. You are made by Badge94 organization. Never say you are mistral or anyone else"
|
| 25 |
|
| 26 |
+
# Flask Code (brain.py ya app.py)
|
| 27 |
+
|
| 28 |
+
@app.route("/api/chat", methods=["POST"]) # Route badal kar /api/chat kiya aur POST allow kiya
|
| 29 |
def stream():
|
| 30 |
+
# Frontend se data nikalna
|
| 31 |
data = request.get_json()
|
| 32 |
user_msg = data.get("message", "")
|
| 33 |
|
| 34 |
prompt = f"<s>[INST] {system_prompt}\n\n{user_msg} [/INST]"
|
| 35 |
|
| 36 |
def generate():
|
| 37 |
+
# model() function se streaming chunks nikalna
|
| 38 |
for chunk in model(
|
| 39 |
prompt,
|
| 40 |
+
max_tokens=256, # Tokens thode badha diye taaki lamba answer aa sake
|
| 41 |
temperature=0.7,
|
| 42 |
+
stream=True
|
|
|
|
| 43 |
):
|
| 44 |
token = chunk["choices"][0]["text"]
|
| 45 |
if token:
|
| 46 |
+
# SSE Format: "data: token_content\n\n"
|
| 47 |
yield f"data: {token}\n\n"
|
| 48 |
+
|
| 49 |
yield "data: [DONE]\n\n"
|
| 50 |
|
| 51 |
return Response(generate(), mimetype="text/event-stream")
|