File size: 831 Bytes
e5b1886
 
 
2fbfc1d
e5b1886
 
 
2fbfc1d
e5b1886
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
303dfad
e5b1886
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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()