import openai
import streamlit as st
import subprocess
# Initialize OpenAI API and server base
openai.api_key = "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"
openai.api_base = "http://zanino.millennium.berkeley.edu:8000/v1"
# Function to get response from Gorilla Server
def get_gorilla_response(prompt, model):
try:
# Create a chat completion using OpenAI API
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("An error occurred:", e)
# Function to extract code from output
def extract_code_from_output(output):
code = output.split("code>>>:")[1]
return code
# Function to execute generated code
def run_generated_code(file_path):
command = ["python", file_path]
try:
# Run the generated code as a subprocess
result = subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
if result.returncode == 0:
st.success("Generated code executed successfully.")
st.code(result.stdout, language="python")
else:
st.error("Generated code execution failed:")
st.code(result.stderr, language="bash")
except Exception as e:
st.error("Error running generated code:", e)
# Set Streamlit layout
st.set_page_config(layout="wide")
# Main function
def main():
st.markdown(
"""