Spaces:
Sleeping
Sleeping
Upload app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,122 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from langchain_groq import ChatGroq
|
| 4 |
+
from langchain_core.prompts import ChatPromptTemplate
|
| 5 |
+
from dotenv import load_dotenv
|
| 6 |
+
|
| 7 |
+
# Load environment variables
|
| 8 |
+
load_dotenv()
|
| 9 |
+
|
| 10 |
+
# List of programming languages
|
| 11 |
+
LANGUAGES = [
|
| 12 |
+
'Python', 'JavaScript', 'Java', 'C++', 'Ruby',
|
| 13 |
+
'Go', 'Rust', 'Swift', 'Kotlin', 'TypeScript',
|
| 14 |
+
'PHP', 'C#', 'Scala', 'Dart', 'Perl'
|
| 15 |
+
]
|
| 16 |
+
|
| 17 |
+
class CodeConverter:
|
| 18 |
+
def __init__(self):
|
| 19 |
+
"""
|
| 20 |
+
Initialize the Groq language model and prompt template
|
| 21 |
+
"""
|
| 22 |
+
# Initialize Groq ChatModel
|
| 23 |
+
self.llm = ChatGroq(
|
| 24 |
+
temperature=0,
|
| 25 |
+
model_name="llama3-groq-8b-8192-tool-use-preview",
|
| 26 |
+
groq_api_key=os.getenv('GROQ_API_KEY')
|
| 27 |
+
)
|
| 28 |
+
|
| 29 |
+
# Create a prompt template for code conversion
|
| 30 |
+
self.prompt = ChatPromptTemplate.from_messages([
|
| 31 |
+
("system", """You are an expert code translator.
|
| 32 |
+
Your task is to convert code from one programming language to another.
|
| 33 |
+
|
| 34 |
+
Guidelines:
|
| 35 |
+
- Preserve the original logic and structure
|
| 36 |
+
- Maintain the same functionality
|
| 37 |
+
- Use idiomatic code for the target language
|
| 38 |
+
- Handle any language-specific nuances
|
| 39 |
+
- Provide clean, readable, and efficient code
|
| 40 |
+
|
| 41 |
+
If direct translation is not possible, provide the most equivalent implementation."""),
|
| 42 |
+
("human", """Convert the code from {source_language} to {target_language}:
|
| 43 |
+
|
| 44 |
+
Code to convert:
|
| 45 |
+
```{source_language}
|
| 46 |
+
{input_code}
|
| 47 |
+
```
|
| 48 |
+
|
| 49 |
+
Please provide the converted code in {target_language}.""")
|
| 50 |
+
])
|
| 51 |
+
|
| 52 |
+
def convert_code(self, input_code, source_lang, target_lang):
|
| 53 |
+
"""
|
| 54 |
+
Convert code between programming languages
|
| 55 |
+
"""
|
| 56 |
+
try:
|
| 57 |
+
# Limit input code to 200 lines
|
| 58 |
+
input_lines = input_code.split('\n')
|
| 59 |
+
if len(input_lines) > 200:
|
| 60 |
+
return "Error: Code exceeds 200 lines limit"
|
| 61 |
+
|
| 62 |
+
# Create the chain
|
| 63 |
+
chain = self.prompt | self.llm
|
| 64 |
+
|
| 65 |
+
# Generate the conversion
|
| 66 |
+
result = chain.invoke({
|
| 67 |
+
"source_language": source_lang,
|
| 68 |
+
"target_language": target_lang,
|
| 69 |
+
"input_code": input_code
|
| 70 |
+
})
|
| 71 |
+
|
| 72 |
+
# Extract and clean the converted code
|
| 73 |
+
converted_code = result.content.strip()
|
| 74 |
+
|
| 75 |
+
# Remove potential code block markdown
|
| 76 |
+
if converted_code.startswith('```') and converted_code.endswith('```'):
|
| 77 |
+
converted_code = converted_code.strip('```').strip()
|
| 78 |
+
|
| 79 |
+
return converted_code
|
| 80 |
+
|
| 81 |
+
except Exception as e:
|
| 82 |
+
return f"An error occurred: {str(e)}"
|
| 83 |
+
|
| 84 |
+
def create_code_converter():
|
| 85 |
+
"""
|
| 86 |
+
Create Gradio interface for code conversion
|
| 87 |
+
"""
|
| 88 |
+
# Initialize code converter
|
| 89 |
+
converter = CodeConverter()
|
| 90 |
+
|
| 91 |
+
# Create Gradio interface
|
| 92 |
+
demo = gr.Interface(
|
| 93 |
+
fn=converter.convert_code,
|
| 94 |
+
inputs=[
|
| 95 |
+
gr.Textbox(
|
| 96 |
+
label="Enter your code",
|
| 97 |
+
lines=10,
|
| 98 |
+
placeholder="Paste your code here (max 200 lines)"
|
| 99 |
+
),
|
| 100 |
+
gr.Dropdown(
|
| 101 |
+
choices=LANGUAGES,
|
| 102 |
+
label="Source Language"
|
| 103 |
+
),
|
| 104 |
+
gr.Dropdown(
|
| 105 |
+
choices=LANGUAGES,
|
| 106 |
+
label="Target Language"
|
| 107 |
+
)
|
| 108 |
+
],
|
| 109 |
+
outputs=gr.Textbox(
|
| 110 |
+
label="Converted Code",
|
| 111 |
+
lines=10
|
| 112 |
+
),
|
| 113 |
+
title="🖥️ Code Converter",
|
| 114 |
+
description="Convert code between different programming languages using Groq and Langchain"
|
| 115 |
+
)
|
| 116 |
+
|
| 117 |
+
return demo
|
| 118 |
+
|
| 119 |
+
# Launch the Gradio app
|
| 120 |
+
if __name__ == "__main__":
|
| 121 |
+
converter = create_code_converter()
|
| 122 |
+
converter.launch(share=True)
|