Spaces:
Sleeping
Sleeping
import gradio as gr | |
import json | |
def chat_completions(request: gr.Request): | |
# Parse the incoming JSON request | |
data = request.json | |
# Create a placeholder response | |
response = { | |
"id": "chatcmpl-123", | |
"object": "chat.completion", | |
"created": 1677652288, | |
"choices": [{ | |
"index": 0, | |
"message": { | |
"role": "assistant", | |
"content": f"Placeholder response. Received: {data['messages'][-1]['content']}" | |
}, | |
"finish_reason": "stop" | |
}], | |
"usage": { | |
"prompt_tokens": 9, | |
"completion_tokens": 12, | |
"total_tokens": 21 | |
} | |
} | |
return json.dumps(response) | |
demo = gr.Interface( | |
fn=chat_completions, | |
inputs=None, | |
outputs=None, | |
title="Chat Completions API", | |
description="Send a POST request to /v1/chat/completions" | |
) | |
if __name__ == "__main__": | |
demo.launch() | |
demo.queue() | |
demo.launch() | |
api = gr.mount_gradio_app(demo, "/v1/chat/completions", api_name="chat_completions") | |