Spaces:
Runtime error
Runtime error
Ilke Ileri commited on
Commit ·
0ccd1fa
1
Parent(s): c6b30d3
Add streaming support for Vapi compatibility
Browse files74 unmodified lines75767778798054 unmodified lines1351361371381391401411421432 unmodified lines1461471481491501511 unmodified line15315415515615715815974 unmodified linestry:data = request.get_json()messages = data.get("messages", [])# Extract user messages (ignore system messages from Vapi)54 unmodified linesif hasattr(model, 'device'):inputs = {k: v.to(model.device) for k, v in inputs.items()}outputs = model.generate(**inputs,max_new_tokens=256,temperature=0.7,do_sample=True,top_p=0.9,2 unmodified linespad_token_id=tokenizer.pad_token_id,eos_token_id=tokenizer.eos_token_id)full_response = tokenizer.decode(outputs[0], skip_special_tokens=False)1 unmodified lineresponse_text = full_response.split("<start_of_turn>model\n")[-1]response_text = response_text.replace("<end_of_turn>", "").strip()# OpenAI-compatible response format (Vapi için tam uyumlu)vapi_response = {"id": "chatcmpl-" + str(hash(prompt))[-10:],"object": "chat.completion",74 unmodified lines757677787980818283848554 unmodified lines1401411421431441451461471481492 unmodified lines1521531541551561571581 unmodified line16016116216316416516616716816917017117217317417517617717817918018118218318418518618718818974 unmodified linestry:data = request.get_json()print(f"Full request data: {data}")# Check if streaming is requestedstream = data.get("stream", False)messages = data.get("messages", [])# Extract user messages (ignore system messages from Vapi)54 unmodified linesif hasattr(model, 'device'):inputs = {k: v.to(model.device) for k, v in inputs.items()}print(f"Generating response...")outputs = model.generate(**inputs,max_new_tokens=150, # Reduced from 256 for faster responsetemperature=0.7,do_sample=True,top_p=0.9,2 unmodified linespad_token_id=tokenizer.pad_token_id,eos_token_id=tokenizer.eos_token_id)print(f"Response generated!")full_response = tokenizer.decode(outputs[0], skip_special_tokens=False)1 unmodified lineresponse_text = full_response.split("<start_of_turn>model\n")[-1]response_text = response_text.replace("<end_of_turn>", "").strip()# If streaming requested, return SSE formatif stream:def generate():# Send the complete response as a single chunk for simplicitychunk = {"id": "chatcmpl-" + str(hash(prompt))[-10:],"object": "chat.completion.chunk","created": int(__import__('time').time()),"model": MODEL_NAME,"choices": [{"index": 0,"delta": {"role": "assistant","content": response_text},"finish_reason": "stop"}]}yield f"data: {__import__('json').dumps(chunk)}\n\n"yield "data: [DONE]\n\n"return app.response_class(generate(), mimetype='text/event-stream')# OpenAI-compatible response format (non-streaming)vapi_response = {"id": "chatcmpl-" + str(hash(prompt))[-10:],"object": "chat.completion",
app.py+32-2