js2perl / app.py
karthigrj's picture
Create app.py
58b2263 verified
import streamlit as st
from langchain.chat_models import ChatOpenAI
from langchain_groq import ChatGroq
# Set up the title of the Streamlit app
st.title("JavaScript to Perl Converter")
groq_api_key = "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"
llm = ChatGroq(groq_api_key=groq_api_key, model_name="llama-3.1-70b-versatile")
# Sidebar for model API key input
# st.sidebar.header("Model Configuration")
# api_key = st.sidebar.text_input("Enter your API Key:", type="password")
# submit_button = st.sidebar.button("Submit API Key")
# Initialize the language model
# llm = None
# if api_key:
# llm = ChatOpenAI(api_key=api_key, model="gpt-4") # Replace with the desired model name
# Main interface
st.subheader("Input JavaScript Code")
javascript_code = st.text_area("Paste your JavaScript code below:", height=600)
convert_button = st.button("Convert to Perl")
# Conversion logic
def convert_to_perl(js_code):
prompt = (
f"Convert the following JavaScript code to an equivalent Perl program. Ensure all logic is preserved, "
f"and Perl best practices are followed. Provide the output in Perl format only, without additional text.\n\n"
f"JavaScript Code:\n{js_code}"
)
if llm:
response = llm.predict(prompt)
return response
else:
return "Please configure the API key in the sidebar."
# Output section
if convert_button:
if not javascript_code.strip():
st.warning("Please enter some JavaScript code to convert.")
elif not llm:
st.warning("API key is required to perform the conversion.")
else:
st.subheader("Converted Perl Code")
perl_code = convert_to_perl(javascript_code)
st.text_area("Output Perl Code", perl_code, height=600)
# Option to download the converted Perl code
st.download_button(
label="Download Perl Code",
data=perl_code,
file_name="converted_code.pl",
mime="text/plain"
)