|
import openai |
|
import gradio as gr |
|
import subprocess |
|
|
|
openai.api_key = "EMPTY" |
|
openai.api_base = "http://zanino.millennium.berkeley.edu:8000/v1" |
|
|
|
|
|
def get_gorilla_response(prompt, model): |
|
try: |
|
completion = openai.ChatCompletion.create( |
|
model=model, |
|
messages=[{"role": "user", "content": prompt}] |
|
) |
|
print("Response: ", completion) |
|
return completion.choices[0].message.content |
|
except Exception as e: |
|
print("Sorry, something went wrong!") |
|
|
|
def extract_code_from_output(output): |
|
code = output.split("code>>>:")[1] |
|
return code |
|
|
|
def run_generated_code(file_path): |
|
|
|
command = ["python", file_path] |
|
try: |
|
|
|
result = subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True) |
|
if result.returncode == 0: |
|
return "Generated code executed successfully.\n" + result.stdout |
|
else: |
|
return "Generated code execution failed with the following error:\n" + result.stderr |
|
except Exception as e: |
|
return "Error occurred while running the generated code: " + str(e) |
|
|
|
def gorilla_magic(openai_key, input_prompt, model_option): |
|
openai.api_key = openai_key |
|
if len(input_prompt) > 0: |
|
result = get_gorilla_response(prompt=input_prompt, model=model_option) |
|
code_result = extract_code_from_output(result) |
|
file_path = f"generated_code_{model_option}.py" |
|
with open(file_path, 'w') as file: |
|
file.write(code_result) |
|
execution_result = run_generated_code(file_path) |
|
return result, code_result, execution_result |
|
|
|
iface = gr.Interface( |
|
fn=gorilla_magic, |
|
inputs=[ |
|
gr.inputs.Textbox(lines=1, placeholder="Enter your OpenAI key here:"), |
|
gr.inputs.Textbox(lines=5, placeholder="Enter your prompt below:"), |
|
gr.inputs.Dropdown(choices=['gorilla-7b-hf-v1', 'gorilla-mpt-7b-hf-v0'], label="Select a model option from the list:") |
|
], |
|
outputs=[ |
|
gr.outputs.Textbox(label="Response"), |
|
gr.outputs.Textbox(label="Generated Code"), |
|
|
|
], |
|
live=True |
|
) |
|
|
|
iface.launch(share=False) |