7jimmy/python_code_genrater
0
1import openai2import streamlit as st3import subprocess4 5# Initialize OpenAI API and server base6openai.api_key = "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"7openai.api_base = "http://zanino.millennium.berkeley.edu:8000/v1"8 9 10# Function to get response from Gorilla Server11def get_gorilla_response(prompt, model):12 try:13 # Create a chat completion using OpenAI API14 completion = openai.ChatCompletion.create(15 model=model,16 messages=[{"role": "user", "content": prompt}]17 )18 print("Response: ", completion)19 return completion.choices[0].message.content20 except Exception as e:21 print("An error occurred:", e)22 23 24# Function to extract code from output25def extract_code_from_output(output):26 code = output.split("code>>>:")[1]27 return code28 29 30# Function to execute generated code31def run_generated_code(file_path):32 command = ["python", file_path]33 try:34 # Run the generated code as a subprocess35 result = subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)36 if result.returncode == 0:37 st.success("Generated code executed successfully.")38 st.code(result.stdout, language="python")39 else:40 st.error("Generated code execution failed:")41 st.code(result.stderr, language="bash")42 except Exception as e:43 st.error("Error running generated code:", e)44 45 46# Set Streamlit layout47st.set_page_config(layout="wide")48 49 50# Main function51def main():52 st.markdown(53 """54 <style>55 .center-image {56 display: flex;57 justify-content: center;58 }59 </style>60 <a href="https://pythonpythonme.netlify.app/index.html">61 <div class="center-image">62 <img src="https://pythonpythonme.netlify.app/PythonPythonME.png" alt="Header image">63 </div>64 </a>65 <p></p>66 <p></p>67 """,68 unsafe_allow_html=True,69 )70 71 # Streamlit app title and input prompt72 st.title("Gorilla LLM App ๐ฆโ๐")73 input_prompt = st.text_area("Enter User prompt:")74 75 # Model selection dropdown76 model_options = ('gorilla-7b-hf-v1', 'gorilla-mpt-7b-hf-v0')77 option = st.selectbox('Select any model:', model_options)78 79 # Button to trigger Gorilla Magic80 if st.button("Generate"):81 if len(input_prompt) > 0:82 # Split app layout into two columns83 col1, col2 = st.columns([1, 1])84 85 # First column: Get Gorilla Server response86 with col1:87 result = get_gorilla_response(prompt=input_prompt, model=option)88 st.write(result)89 90 # Second column: Display generated code91 with col2:92 def extract_code_from_output(result):93 if result is not None:94 output = result.stdout95 if output is not None and "code>>>:" in output:96 code = output.split("code>>>:")[1]97 return code98 return None # or handle the case when code extraction is not successful99 100# Example usage:101 result = ... # your result variable102 code_result = extract_code_from_output(result)103 104 code_result = extract_code_from_output(result)105 if option == "gorilla-7b-hf-v1":106 st.subheader("Output Generated")107 st.code(code_result, language='python')108 elif option == "gorilla-mpt-7b-hf-v0":109 lines = code_result.split('\\n')110 for line in lines[:-1]:111 st.code(line, language='python')112 113 file_path = f"generated_code_{option.replace('-', '_')}.py"114 with open(file_path, 'w') as file:115 file.write(code_result)116 run_generated_code(file_path)117 118 st.markdown(119 '''120 <style>121 .center-image {122 display: flex;123 justify-content: center;124 }125 .follow-me {126 text-align: center;127 }128 .social-icons {129 display: flex;130 justify-content: center;131 list-style: none;132 padding: 0;133 }134 .social-icons li {135 margin: 0 10px;136 }137 </style>138 <body>139 <div class="center-image">140 <h4>Anoop Johny ๐ค</h4>141 </div>142 <div class="center-image">143 <h4>Follow Me</h4>144 </div>145 <div class="center-image">146 <ul class="social-icons">147 <li><a href="https://www.linkedin.com/in/anoop-johny-30a746181/"><img src="https://pythonpythonme.netlify.app/static/res/linkedin.png" width="55" height="55" alt="LinkedIn"></a></li>148 <li><a href="https://github.com/flyfir248"><img src="https://pythonpythonme.netlify.app/static/res/github.png" width="55" height="55" alt="GitHub"></a></li>149 <li><a href="https://pythonpythonme.netlify.app/index.html"><img src="https://pythonpythonme.netlify.app/static/res/web.png" width="55" height="55" alt="Website"></a></li>150 <li><a href="https://medium.com/@anoopjohny2000"><img src="https://pythonpythonme.netlify.app/static/res/medium.png" width="55" height="55" alt="Medium"></a></li>151 <li><a href="https://www.kooapp.com/profile/anoop2DEVLJ"><img src="https://www.kooapp.com/_next/static/media/logoKuSolidOutline.1f4fa971.svg" width="55" height="55" alt="The Koo App" width="55" height="55"></a></li>152 </ul>153 </div>154 <footer class="footer">155 <div class="container">156 <div class="row">157 <div class="center-image">158 <p class="text-muted">ยฉ 2023-2024 PythonPythonME.</p>159 <p>All rights reserved.</p>160 </div>161 </div>162 </div>163 </footer>164 </body>165 ''',166 unsafe_allow_html=True167 )168 169if __name__ == '__main__':170 main()171 