Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from langchain.chat_models import ChatOpenAI
|
| 3 |
+
from langchain_groq import ChatGroq
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
# Set up the title of the Streamlit app
|
| 7 |
+
st.title("JavaScript to Perl Converter")
|
| 8 |
+
groq_api_key = "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"
|
| 9 |
+
llm = ChatGroq(groq_api_key=groq_api_key, model_name="llama-3.1-70b-versatile")
|
| 10 |
+
# Sidebar for model API key input
|
| 11 |
+
# st.sidebar.header("Model Configuration")
|
| 12 |
+
# api_key = st.sidebar.text_input("Enter your API Key:", type="password")
|
| 13 |
+
# submit_button = st.sidebar.button("Submit API Key")
|
| 14 |
+
|
| 15 |
+
# Initialize the language model
|
| 16 |
+
# llm = None
|
| 17 |
+
# if api_key:
|
| 18 |
+
# llm = ChatOpenAI(api_key=api_key, model="gpt-4") # Replace with the desired model name
|
| 19 |
+
|
| 20 |
+
# Main interface
|
| 21 |
+
st.subheader("Input JavaScript Code")
|
| 22 |
+
javascript_code = st.text_area("Paste your JavaScript code below:", height=600)
|
| 23 |
+
|
| 24 |
+
convert_button = st.button("Convert to Perl")
|
| 25 |
+
|
| 26 |
+
# Conversion logic
|
| 27 |
+
def convert_to_perl(js_code):
|
| 28 |
+
prompt = (
|
| 29 |
+
f"Convert the following JavaScript code to an equivalent Perl program. Ensure all logic is preserved, "
|
| 30 |
+
f"and Perl best practices are followed. Provide the output in Perl format only, without additional text.\n\n"
|
| 31 |
+
f"JavaScript Code:\n{js_code}"
|
| 32 |
+
)
|
| 33 |
+
if llm:
|
| 34 |
+
response = llm.predict(prompt)
|
| 35 |
+
return response
|
| 36 |
+
else:
|
| 37 |
+
return "Please configure the API key in the sidebar."
|
| 38 |
+
|
| 39 |
+
# Output section
|
| 40 |
+
if convert_button:
|
| 41 |
+
if not javascript_code.strip():
|
| 42 |
+
st.warning("Please enter some JavaScript code to convert.")
|
| 43 |
+
elif not llm:
|
| 44 |
+
st.warning("API key is required to perform the conversion.")
|
| 45 |
+
else:
|
| 46 |
+
st.subheader("Converted Perl Code")
|
| 47 |
+
perl_code = convert_to_perl(javascript_code)
|
| 48 |
+
st.text_area("Output Perl Code", perl_code, height=600)
|
| 49 |
+
|
| 50 |
+
# Option to download the converted Perl code
|
| 51 |
+
st.download_button(
|
| 52 |
+
label="Download Perl Code",
|
| 53 |
+
data=perl_code,
|
| 54 |
+
file_name="converted_code.pl",
|
| 55 |
+
mime="text/plain"
|
| 56 |
+
)
|