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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import openai
|
| 3 |
+
|
| 4 |
+
# --- UI Configuration ---
|
| 5 |
+
|
| 6 |
+
# Extracted from the provided React code
|
| 7 |
+
LANGUAGES = [
|
| 8 |
+
'Python', 'JavaScript', 'TypeScript', 'Java', 'C++', 'C#', 'C', 'Go',
|
| 9 |
+
'Rust', 'Swift', 'Kotlin', 'PHP', 'Ruby', 'Scala', 'R', 'MATLAB',
|
| 10 |
+
'Perl', 'Haskell', 'Lua', 'Dart', 'Elixir', 'F#', 'Clojure',
|
| 11 |
+
'Objective-C', 'Visual Basic', 'Bash', 'PowerShell', 'SQL', 'HTML',
|
| 12 |
+
'CSS', 'JSON', 'XML', 'YAML', 'Assembly', 'Fortran', 'COBOL',
|
| 13 |
+
'Pascal', 'Delphi', 'Groovy', 'Julia', 'Nim', 'Crystal', 'Zig',
|
| 14 |
+
'V', 'OCaml', 'Erlang', 'Prolog', 'Lisp', 'Scheme', 'Racket'
|
| 15 |
+
]
|
| 16 |
+
|
| 17 |
+
# A selection of popular and capable models available via OpenRouter
|
| 18 |
+
# You can expand this list with other models from openrouter.ai
|
| 19 |
+
MODELS = [
|
| 20 |
+
"anthropic/claude-3.5-sonnet",
|
| 21 |
+
"google/gemini-flash-1.5",
|
| 22 |
+
"meta-llama/llama-3-8b-instruct",
|
| 23 |
+
"mistralai/mistral-large",
|
| 24 |
+
"openai/gpt-4o",
|
| 25 |
+
"microsoft/wizardlm-2-8x22b",
|
| 26 |
+
]
|
| 27 |
+
|
| 28 |
+
# Default example code from the original application
|
| 29 |
+
DEFAULT_SOURCE_CODE = """# Example Python code
|
| 30 |
+
def fibonacci(n):
|
| 31 |
+
if n <= 1:
|
| 32 |
+
return n
|
| 33 |
+
return fibonacci(n-1) + fibonacci(n-2)
|
| 34 |
+
|
| 35 |
+
print(fibonacci(10))"""
|
| 36 |
+
|
| 37 |
+
# --- Core Conversion Logic ---
|
| 38 |
+
|
| 39 |
+
def convert_code(
|
| 40 |
+
source_code: str,
|
| 41 |
+
source_lang: str,
|
| 42 |
+
target_lang: str,
|
| 43 |
+
model: str,
|
| 44 |
+
api_key: str,
|
| 45 |
+
):
|
| 46 |
+
"""
|
| 47 |
+
Uses an LLM via OpenRouter to convert code from one language to another.
|
| 48 |
+
"""
|
| 49 |
+
if not source_code.strip():
|
| 50 |
+
raise gr.Error("Please enter some code to convert.")
|
| 51 |
+
if not api_key.strip():
|
| 52 |
+
raise gr.Error("OpenRouter API key is required.")
|
| 53 |
+
if not model:
|
| 54 |
+
raise gr.Error("Please select a model for the conversion.")
|
| 55 |
+
|
| 56 |
+
# Initialize the OpenAI client to point to OpenRouter's API
|
| 57 |
+
client = openai.OpenAI(
|
| 58 |
+
base_url="https://openrouter.ai/api/v1",
|
| 59 |
+
api_key=api_key,
|
| 60 |
+
)
|
| 61 |
+
|
| 62 |
+
# Construct the prompt for the LLM
|
| 63 |
+
prompt = (
|
| 64 |
+
f"You are an expert programmer. Convert the following {source_lang} code "
|
| 65 |
+
f"to {target_lang}. Your response should only contain the raw, converted code. "
|
| 66 |
+
"Do not include any explanations, markdown formatting (like ```), or extra text."
|
| 67 |
+
f"\n\n--- Start of {source_lang} Code ---\n"
|
| 68 |
+
f"{source_code}\n"
|
| 69 |
+
f"--- End of {source_lang} Code ---"
|
| 70 |
+
)
|
| 71 |
+
|
| 72 |
+
try:
|
| 73 |
+
# Make the API call to the selected model
|
| 74 |
+
completion = client.chat.completions.create(
|
| 75 |
+
model=model,
|
| 76 |
+
messages=[
|
| 77 |
+
{"role": "user", "content": prompt},
|
| 78 |
+
],
|
| 79 |
+
temperature=0.1, # Lower temperature for more deterministic output
|
| 80 |
+
max_tokens=2048,
|
| 81 |
+
)
|
| 82 |
+
converted_code = completion.choices[0].message.content.strip()
|
| 83 |
+
return converted_code
|
| 84 |
+
|
| 85 |
+
except openai.AuthenticationError:
|
| 86 |
+
raise gr.Error("Authentication failed. Please check your OpenRouter API key.")
|
| 87 |
+
except Exception as e:
|
| 88 |
+
# A catch-all for other potential API or network errors
|
| 89 |
+
raise gr.Error(f"An error occurred during conversion: {e}")
|
| 90 |
+
|
| 91 |
+
# --- Gradio User Interface ---
|
| 92 |
+
|
| 93 |
+
with gr.Blocks(
|
| 94 |
+
theme=gr.themes.Monochrome(),
|
| 95 |
+
css=".gradio-container {background-color: #0f172a}" # Dark theme like the original
|
| 96 |
+
) as app:
|
| 97 |
+
# Header
|
| 98 |
+
with gr.Row():
|
| 99 |
+
gr.HTML(
|
| 100 |
+
"""
|
| 101 |
+
<div style="display: flex; align-items: center; gap: 12px; padding: 10px;">
|
| 102 |
+
<svg xmlns="[http://www.w3.org/2000/svg](http://www.w3.org/2000/svg)" width="32" height="32" viewBox="0 0 24 24" fill="none" stroke="#3b82f6" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-code-xml"><path d="m18 16 4-4-4-4"/><path d="m6 8-4 4 4 4"/><path d="m14.5 4-5 16"/></svg>
|
| 103 |
+
<div>
|
| 104 |
+
<h1 style="font-size: 1.5rem; font-weight: 700; color: #ffffff; margin: 0;">CodeVerter</h1>
|
| 105 |
+
<p style="font-size: 0.875rem; color: #9ca3af; margin: 0;">Programming Language Converter via OpenRouter</p>
|
| 106 |
+
</div>
|
| 107 |
+
</div>
|
| 108 |
+
"""
|
| 109 |
+
)
|
| 110 |
+
|
| 111 |
+
# API Key and Model Selection
|
| 112 |
+
with gr.Accordion("⚙️ Configuration", open=True):
|
| 113 |
+
openrouter_key = gr.Textbox(
|
| 114 |
+
label="OpenRouter API Key",
|
| 115 |
+
placeholder="Enter your OpenRouter API key here...",
|
| 116 |
+
type="password",
|
| 117 |
+
elem_id="api-key-textbox",
|
| 118 |
+
)
|
| 119 |
+
model_selection = gr.Dropdown(
|
| 120 |
+
label="Select LLM Model",
|
| 121 |
+
choices=MODELS,
|
| 122 |
+
value=MODELS[0], # Default to the first model in the list
|
| 123 |
+
)
|
| 124 |
+
|
| 125 |
+
# Main conversion interface
|
| 126 |
+
with gr.Row(equal_height=False):
|
| 127 |
+
# Input column
|
| 128 |
+
with gr.Column(scale=1):
|
| 129 |
+
source_lang_selection = gr.Dropdown(
|
| 130 |
+
label="Source Language",
|
| 131 |
+
choices=LANGUAGES,
|
| 132 |
+
value="Python",
|
| 133 |
+
)
|
| 134 |
+
source_code_input = gr.Code(
|
| 135 |
+
label="Source Code",
|
| 136 |
+
language="python", # Start with python syntax highlighting
|
| 137 |
+
value=DEFAULT_SOURCE_CODE,
|
| 138 |
+
lines=15,
|
| 139 |
+
)
|
| 140 |
+
|
| 141 |
+
# Output column
|
| 142 |
+
with gr.Column(scale=1):
|
| 143 |
+
target_lang_selection = gr.Dropdown(
|
| 144 |
+
label="Target Language",
|
| 145 |
+
choices=LANGUAGES,
|
| 146 |
+
value="JavaScript",
|
| 147 |
+
)
|
| 148 |
+
target_code_output = gr.Code(
|
| 149 |
+
label="Converted Code",
|
| 150 |
+
language="javascript", # Start with javascript syntax highlighting
|
| 151 |
+
lines=15,
|
| 152 |
+
interactive=False, # Make it read-only
|
| 153 |
+
)
|
| 154 |
+
|
| 155 |
+
# Keep language highlighting in sync with dropdowns
|
| 156 |
+
source_lang_selection.change(
|
| 157 |
+
lambda x: gr.update(language=x.lower()),
|
| 158 |
+
inputs=source_lang_selection,
|
| 159 |
+
outputs=source_code_input
|
| 160 |
+
)
|
| 161 |
+
target_lang_selection.change(
|
| 162 |
+
lambda x: gr.update(language=x.lower()),
|
| 163 |
+
inputs=target_lang_selection,
|
| 164 |
+
outputs=target_code_output
|
| 165 |
+
)
|
| 166 |
+
|
| 167 |
+
# Convert button and action
|
| 168 |
+
convert_button = gr.Button("Convert Code", variant="primary")
|
| 169 |
+
convert_button.click(
|
| 170 |
+
fn=convert_code,
|
| 171 |
+
inputs=[
|
| 172 |
+
source_code_input,
|
| 173 |
+
source_lang_selection,
|
| 174 |
+
target_lang_selection,
|
| 175 |
+
model_selection,
|
| 176 |
+
openrouter_key,
|
| 177 |
+
],
|
| 178 |
+
outputs=target_code_output,
|
| 179 |
+
api_name="convert"
|
| 180 |
+
)
|
| 181 |
+
|
| 182 |
+
# Footer
|
| 183 |
+
gr.HTML(
|
| 184 |
+
"""
|
| 185 |
+
<div style="text-align: center; margin-top: 24px; color: #9ca3af; font-size: 0.875rem;">
|
| 186 |
+
<p>CodeVerter uses LLMs via OpenRouter to convert code. Results may require manual review.</p>
|
| 187 |
+
</div>
|
| 188 |
+
"""
|
| 189 |
+
)
|
| 190 |
+
|
| 191 |
+
if __name__ == "__main__":
|
| 192 |
+
app.launch()
|