Spaces:
Sleeping
Sleeping
File size: 6,270 Bytes
bd0a75c bcb904b bd0a75c 5dd6360 bd0a75c |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
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(
"""
<style>
.center-image {
display: flex;
justify-content: center;
}
</style>
<a href="https://pythonpythonme.netlify.app/index.html">
<div class="center-image">
<img src="https://pythonpythonme.netlify.app/PythonPythonME.png" alt="Header image">
</div>
</a>
<p></p>
<p></p>
""",
unsafe_allow_html=True,
)
# Streamlit app title and input prompt
st.title("Gorilla LLM App π¦βπ")
input_prompt = st.text_area("Enter User prompt:")
# Model selection dropdown
model_options = ('gorilla-7b-hf-v1', 'gorilla-mpt-7b-hf-v0')
option = st.selectbox('Select any model:', model_options)
# Button to trigger Gorilla Magic
if st.button("Generate"):
if len(input_prompt) > 0:
# Split app layout into two columns
col1, col2 = st.columns([1, 1])
# First column: Get Gorilla Server response
with col1:
result = get_gorilla_response(prompt=input_prompt, model=option)
st.write(result)
# Second column: Display generated code
with col2:
def extract_code_from_output(result):
if result is not None:
output = result.stdout
if output is not None and "code>>>:" in output:
code = output.split("code>>>:")[1]
return code
return None # or handle the case when code extraction is not successful
# Example usage:
result = ... # your result variable
code_result = extract_code_from_output(result)
code_result = extract_code_from_output(result)
if option == "gorilla-7b-hf-v1":
st.subheader("Output Generated")
st.code(code_result, language='python')
elif option == "gorilla-mpt-7b-hf-v0":
lines = code_result.split('\\n')
for line in lines[:-1]:
st.code(line, language='python')
file_path = f"generated_code_{option.replace('-', '_')}.py"
with open(file_path, 'w') as file:
file.write(code_result)
run_generated_code(file_path)
st.markdown(
'''
<style>
.center-image {
display: flex;
justify-content: center;
}
.follow-me {
text-align: center;
}
.social-icons {
display: flex;
justify-content: center;
list-style: none;
padding: 0;
}
.social-icons li {
margin: 0 10px;
}
</style>
<body>
<div class="center-image">
<h4>Anoop Johny π€</h4>
</div>
<div class="center-image">
<h4>Follow Me</h4>
</div>
<div class="center-image">
<ul class="social-icons">
<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>
<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>
<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>
<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>
<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>
</ul>
</div>
<footer class="footer">
<div class="container">
<div class="row">
<div class="center-image">
<p class="text-muted">Β© 2023-2024 PythonPythonME.</p>
<p>All rights reserved.</p>
</div>
</div>
</div>
</footer>
</body>
''',
unsafe_allow_html=True
)
if __name__ == '__main__':
main()
|