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'
' else: lines[i]= '' elif "" in line and "
" not in line:
lines[i] = "
"
elif "#" in line:
lines[i] = f"{line.strip('#')}
"
elif "##" in line:
lines[i] = f"{line.strip('##')}
"
elif "###" in line:
lines[i] = f"{line.strip('###')}
"
return "\n".join(lines)
def generate_response(prompt):
# procesăm promptul
prompt = parse_text(prompt)
# configurăm OpenAI API
openai.api_key = "API_KEY_HERE"
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()