import gradio as gr from openai import OpenAI def streaming(api_key, template, prompt): openai_client = OpenAI(api_key=api_key) messages = [ {"role": "system", "content": template}, {"role": "user", "content": prompt} ] response = openai_client.chat.completions.create( model="gpt-4-turbo-preview", messages=messages, temperature = 0, stream = True ) full_response = "" for chunk in response: if chunk.choices[0].delta.content is not None: full_response += chunk.choices[0].delta.content yield full_response inputs = [ gr.Textbox(label="API KEY"), gr.Textbox(label = 'Template'), gr.Textbox(label="Prompt") ] output = gr.Textbox() app = gr.Interface(streaming, inputs, output) app.launch()