Create app.py (#1)
Browse files- Create app.py (f23942a0d6fd25aff5716f28128120b6fcca93c2)
app.py
ADDED
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import openai
|
2 |
+
import gradio as gr
|
3 |
+
import subprocess
|
4 |
+
|
5 |
+
openai.api_key = "EMPTY" # Key is ignored and does not matter
|
6 |
+
openai.api_base = "http://zanino.millennium.berkeley.edu:8000/v1"
|
7 |
+
|
8 |
+
# Query Gorilla Server
|
9 |
+
def get_gorilla_response(prompt, model):
|
10 |
+
try:
|
11 |
+
completion = openai.ChatCompletion.create(
|
12 |
+
model=model,
|
13 |
+
messages=[{"role": "user", "content": prompt}]
|
14 |
+
)
|
15 |
+
print("Response: ", completion)
|
16 |
+
return completion.choices[0].message.content
|
17 |
+
except Exception as e:
|
18 |
+
print("Sorry, something went wrong!")
|
19 |
+
|
20 |
+
def extract_code_from_output(output):
|
21 |
+
code = output.split("code>>>:")[1]
|
22 |
+
return code
|
23 |
+
|
24 |
+
def run_generated_code(file_path):
|
25 |
+
# Command to run the generated code using Python interpreter
|
26 |
+
command = ["python", file_path]
|
27 |
+
try:
|
28 |
+
# Execute the command as a subprocess and capture the output and error streams
|
29 |
+
result = subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
|
30 |
+
if result.returncode == 0:
|
31 |
+
return "Generated code executed successfully.\n" + result.stdout
|
32 |
+
else:
|
33 |
+
return "Generated code execution failed with the following error:\n" + result.stderr
|
34 |
+
except Exception as e:
|
35 |
+
return "Error occurred while running the generated code: " + str(e)
|
36 |
+
|
37 |
+
def gorilla_magic(openai_key, input_prompt, model_option):
|
38 |
+
openai.api_key = openai_key
|
39 |
+
if len(input_prompt) > 0:
|
40 |
+
result = get_gorilla_response(prompt=input_prompt, model=model_option)
|
41 |
+
code_result = extract_code_from_output(result)
|
42 |
+
file_path = f"generated_code_{model_option}.py"
|
43 |
+
with open(file_path, 'w') as file:
|
44 |
+
file.write(code_result)
|
45 |
+
execution_result = run_generated_code(file_path)
|
46 |
+
return result, code_result, execution_result
|
47 |
+
|
48 |
+
iface = gr.Interface(
|
49 |
+
fn=gorilla_magic,
|
50 |
+
inputs=[
|
51 |
+
gr.inputs.Textbox(lines=1, placeholder="Enter your OpenAI key here:"),
|
52 |
+
gr.inputs.Textbox(lines=5, placeholder="Enter your prompt below:"),
|
53 |
+
gr.inputs.Dropdown(choices=['gorilla-7b-hf-v1', 'gorilla-mpt-7b-hf-v0'], label="Select a model option from the list:")
|
54 |
+
],
|
55 |
+
outputs=[
|
56 |
+
gr.outputs.Textbox(label="Response"),
|
57 |
+
gr.outputs.Textbox(label="Generated Code"), # 使用 Textbox 來顯示代碼
|
58 |
+
# gr.outputs.Textbox(label="Execution Result")
|
59 |
+
],
|
60 |
+
live=True
|
61 |
+
)
|
62 |
+
|
63 |
+
iface.launch(share=False)
|