File size: 1,787 Bytes
9327ec1
0951c40
 
afc461f
 
0951c40
 
 
 
ff45051
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
afc461f
 
ff45051
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
afc461f
ff45051
afc461f
ff45051
 
 
 
 
 
 
afc461f
 
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import gradio as gr
import openai
import os

css_file = os.path.join(os.path.dirname(file), "style.css")

initial_prompt = "You are a helpful assistant."

def parse_text(text):
    lines = text.split("\n")
    for i, line in enumerate(lines):
        if "`" in line:
            items = line.split('`')
            if items[-1]:
                lines[i] = f'<pre><code class="{items[-1]}">'
            else:
                lines[i] = '<pre><code>'
        elif "`" in line and "</code></pre>" not in line:
            lines[i] = "</code></pre>"
        elif "#" in line:
            lines[i] = f"<h1>{line.strip('#')}</h1>"
        elif "##" in line:
            lines[i] = f"<h2>{line.strip('##')}</h2>"
        elif "###" in line:
            lines[i] = f"<h3>{line.strip('###')}</h3>"
    return "\n".join(lines)

def generate_response(prompt):
    # procesăm promptul
    prompt = parse_text(prompt)
    # configurăm OpenAI API
    openai.api_key = "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"
    model_engine = "davinci"
    prompt_length = len(prompt)
    max_tokens = 1024 - prompt_length
    temperature = 0.7
    #generăm răspunsul folosind OpenAI API
    response = openai.Completion.create(
        engine=model_engine,
        prompt=prompt,
        max_tokens=max_tokens,
        n=1,
        temperature=temperature,
    )
    message = response.choices[0].text.strip()
    # procesăm răspunsul generat
    message = parse_text(message)
    return message

# definim interfața Gradio
iface = gr.Interface(
    fn=generate_response,
    inputs=gr.inputs.Textbox(lines=7, label="Input prompt"),
    outputs=gr.outputs.HTML(label="Model response"),
    title="OpenAI GPT-3 assistant",
    layout="vertical",
    width=800,
    css=css_file,
)
iface.launch()