import gradio as gr import openai from fastapi import FastAPI, Request import uvicorn import json openai.api_key = "" app = FastAPI() conversation_history = [] @app.post("/chat") async def chat(request: Request): global conversation_history message = await request.json() # Convert the user's message to JSON message_json = json.dumps(message) conversation_history.append({"role": "user", "content": message_json}) # Prepare the messages for the GPT-3 API call messages = conversation_history.copy() # Add a system message to instruct the model messages.insert(0, {"role": "system", "content": "You are a story writer. What ever the prompt you always write a story"}) completion = openai.ChatCompletion.create( model="gpt-4", messages=messages ) reply = completion.choices[0].message.content # Append the model's reply to the conversation history conversation_history.append({"role": "assistant", "content": reply}) return reply def gradio_chat(message): reply = chat(message) return reply with gr.ChatInterface(fn=gradio_chat) as chatbot: chatbot.launch(server_name="0.0.0.0", server_port=7860) # Run the FastAPI app if __name__ == "__main__": uvicorn.run(app, host="0.0.0.0", port=7860)