Spaces:
Build error
Build error
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() |