Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -37,17 +37,26 @@ class NordTheme(Base):
|
|
| 37 |
background_fill_primary_dark="#2E3440",
|
| 38 |
color_accent_soft="#4c566a",
|
| 39 |
block_radius="12px",
|
| 40 |
-
button_radius
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
)
|
| 42 |
|
| 43 |
# --- UI Configuration ---
|
| 44 |
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
'
|
| 48 |
-
'
|
| 49 |
-
'
|
| 50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
|
| 52 |
MODELS = sorted([
|
| 53 |
"agentica-org/deepcoder-14b-preview:free", "deepseek/deepseek-chat-v3:free",
|
|
@@ -84,23 +93,19 @@ def convert_code(
|
|
| 84 |
raise gr.Error("OpenRouter API key is required.")
|
| 85 |
if not model or not model.strip():
|
| 86 |
raise gr.Error("Please select or enter a model for the conversion.")
|
| 87 |
-
|
| 88 |
-
# Handle the edge case where both are "Natural Language"
|
| 89 |
if source_lang == "Natural Language" and target_lang == "Natural Language":
|
| 90 |
raise gr.Error("Please select a programming language for either the source or the target.")
|
| 91 |
|
| 92 |
client = openai.OpenAI(base_url="https://openrouter.ai/api/v1", api_key=api_key)
|
| 93 |
|
| 94 |
-
# UPDATED: Logic to generate a prompt based on the conversion type
|
| 95 |
if source_lang == "Natural Language":
|
| 96 |
-
# Scenario 1: Natural Language -> Code
|
| 97 |
prompt = (
|
| 98 |
f"You are a programming expert. Based on the following description, write a complete and functional code snippet in {target_lang}. "
|
| 99 |
f"The user's request is: '{source_code}'.\n\n"
|
| 100 |
f"Your response must only contain the raw {target_lang} code without any explanations, comments, or markdown formatting (like ```)."
|
| 101 |
)
|
| 102 |
elif target_lang == "Natural Language":
|
| 103 |
-
# Scenario 2: Code -> Natural Language
|
| 104 |
prompt = (
|
| 105 |
f"You are a programming expert. Explain the following {source_lang} code in simple, natural English. "
|
| 106 |
f"Describe what the code does, its main logic, its inputs, and what it outputs. Do not include any code in your explanation.\n\n"
|
|
@@ -109,7 +114,6 @@ def convert_code(
|
|
| 109 |
f"--- End of {source_lang} Code ---"
|
| 110 |
)
|
| 111 |
else:
|
| 112 |
-
# Scenario 3: Code -> Code (original logic)
|
| 113 |
prompt = (
|
| 114 |
f"You are an expert programmer. Convert the following {source_lang} code to {target_lang}. "
|
| 115 |
"Your response must only contain the raw, converted code. Do not include any explanations, comments, or markdown formatting (like ```)."
|
|
@@ -117,7 +121,7 @@ def convert_code(
|
|
| 117 |
f"{source_code}\n"
|
| 118 |
f"--- End of {source_lang} Code ---"
|
| 119 |
)
|
| 120 |
-
|
| 121 |
try:
|
| 122 |
completion = client.chat.completions.create(
|
| 123 |
model=model,
|
|
@@ -132,14 +136,13 @@ def convert_code(
|
|
| 132 |
|
| 133 |
# --- Gradio User Interface ---
|
| 134 |
|
| 135 |
-
# Helper function to update Code component's language
|
| 136 |
def update_code_language(lang: str):
|
|
|
|
| 137 |
if lang == "Natural Language":
|
| 138 |
return gr.update(language="text", placeholder="Describe the code you want here...")
|
| 139 |
return gr.update(language=lang.lower(), placeholder=f"Enter your {lang} code here...")
|
| 140 |
|
| 141 |
with gr.Blocks(theme=NordTheme()) as app:
|
| 142 |
-
# Header
|
| 143 |
gr.HTML(
|
| 144 |
"""
|
| 145 |
<div style="display: flex; align-items: center; gap: 12px; padding: 10px;">
|
|
@@ -163,8 +166,7 @@ with gr.Blocks(theme=NordTheme()) as app:
|
|
| 163 |
with gr.Column(scale=1):
|
| 164 |
target_lang_selection = gr.Dropdown(label="Target Language", choices=LANGUAGES, value="JavaScript")
|
| 165 |
target_code_output = gr.Code(label="Result", language="javascript", lines=15, interactive=False)
|
| 166 |
-
|
| 167 |
-
# UPDATED: Use a helper function for cleaner UI logic
|
| 168 |
source_lang_selection.change(fn=update_code_language, inputs=source_lang_selection, outputs=source_code_input)
|
| 169 |
target_lang_selection.change(lambda lang: gr.update(language=lang.lower() if lang != "Natural Language" else "text"), inputs=target_lang_selection, outputs=target_code_output)
|
| 170 |
|
|
|
|
| 37 |
background_fill_primary_dark="#2E3440",
|
| 38 |
color_accent_soft="#4c566a",
|
| 39 |
block_radius="12px",
|
| 40 |
+
# The invalid 'button_radius' argument has been removed.
|
| 41 |
+
# Button radius is now controlled by the properties below.
|
| 42 |
+
radius_sm=sizes.radius_sm,
|
| 43 |
+
radius_md=sizes.radius_md,
|
| 44 |
+
radius_lg=sizes.radius_lg,
|
| 45 |
)
|
| 46 |
|
| 47 |
# --- UI Configuration ---
|
| 48 |
|
| 49 |
+
PROGRAMMING_LANGUAGES = sorted([
|
| 50 |
+
'Ada', 'Assembly', 'Bash', 'C', 'C#', 'C++', 'Clojure', 'COBOL', 'CSS',
|
| 51 |
+
'Crystal', 'Dart', 'Elixir', 'F#', 'Fortran', 'Go', 'GraphQL', 'Groovy',
|
| 52 |
+
'Haskell', 'HTML', 'Java', 'JavaScript', 'Julia', 'Kotlin', 'Lisp', 'Lua',
|
| 53 |
+
'Markdown', 'MATLAB', 'Nim', 'Objective-C', 'OCaml', 'Pascal', 'Perl', 'PHP',
|
| 54 |
+
'PowerShell', 'Prolog', 'Python', 'R', 'Ruby', 'Rust', 'Scala', 'Scheme',
|
| 55 |
+
'SQL', 'Svelte', 'Swift', 'TOML', 'TypeScript', 'Visual Basic', 'Vue',
|
| 56 |
+
'XML', 'YAML', 'Zig'
|
| 57 |
+
])
|
| 58 |
+
LANGUAGES = ['Natural Language'] + PROGRAMMING_LANGUAGES
|
| 59 |
+
|
| 60 |
|
| 61 |
MODELS = sorted([
|
| 62 |
"agentica-org/deepcoder-14b-preview:free", "deepseek/deepseek-chat-v3:free",
|
|
|
|
| 93 |
raise gr.Error("OpenRouter API key is required.")
|
| 94 |
if not model or not model.strip():
|
| 95 |
raise gr.Error("Please select or enter a model for the conversion.")
|
| 96 |
+
|
|
|
|
| 97 |
if source_lang == "Natural Language" and target_lang == "Natural Language":
|
| 98 |
raise gr.Error("Please select a programming language for either the source or the target.")
|
| 99 |
|
| 100 |
client = openai.OpenAI(base_url="https://openrouter.ai/api/v1", api_key=api_key)
|
| 101 |
|
|
|
|
| 102 |
if source_lang == "Natural Language":
|
|
|
|
| 103 |
prompt = (
|
| 104 |
f"You are a programming expert. Based on the following description, write a complete and functional code snippet in {target_lang}. "
|
| 105 |
f"The user's request is: '{source_code}'.\n\n"
|
| 106 |
f"Your response must only contain the raw {target_lang} code without any explanations, comments, or markdown formatting (like ```)."
|
| 107 |
)
|
| 108 |
elif target_lang == "Natural Language":
|
|
|
|
| 109 |
prompt = (
|
| 110 |
f"You are a programming expert. Explain the following {source_lang} code in simple, natural English. "
|
| 111 |
f"Describe what the code does, its main logic, its inputs, and what it outputs. Do not include any code in your explanation.\n\n"
|
|
|
|
| 114 |
f"--- End of {source_lang} Code ---"
|
| 115 |
)
|
| 116 |
else:
|
|
|
|
| 117 |
prompt = (
|
| 118 |
f"You are an expert programmer. Convert the following {source_lang} code to {target_lang}. "
|
| 119 |
"Your response must only contain the raw, converted code. Do not include any explanations, comments, or markdown formatting (like ```)."
|
|
|
|
| 121 |
f"{source_code}\n"
|
| 122 |
f"--- End of {source_lang} Code ---"
|
| 123 |
)
|
| 124 |
+
|
| 125 |
try:
|
| 126 |
completion = client.chat.completions.create(
|
| 127 |
model=model,
|
|
|
|
| 136 |
|
| 137 |
# --- Gradio User Interface ---
|
| 138 |
|
|
|
|
| 139 |
def update_code_language(lang: str):
|
| 140 |
+
"""Updates the Code component's language and placeholder text."""
|
| 141 |
if lang == "Natural Language":
|
| 142 |
return gr.update(language="text", placeholder="Describe the code you want here...")
|
| 143 |
return gr.update(language=lang.lower(), placeholder=f"Enter your {lang} code here...")
|
| 144 |
|
| 145 |
with gr.Blocks(theme=NordTheme()) as app:
|
|
|
|
| 146 |
gr.HTML(
|
| 147 |
"""
|
| 148 |
<div style="display: flex; align-items: center; gap: 12px; padding: 10px;">
|
|
|
|
| 166 |
with gr.Column(scale=1):
|
| 167 |
target_lang_selection = gr.Dropdown(label="Target Language", choices=LANGUAGES, value="JavaScript")
|
| 168 |
target_code_output = gr.Code(label="Result", language="javascript", lines=15, interactive=False)
|
| 169 |
+
|
|
|
|
| 170 |
source_lang_selection.change(fn=update_code_language, inputs=source_lang_selection, outputs=source_code_input)
|
| 171 |
target_lang_selection.change(lambda lang: gr.update(language=lang.lower() if lang != "Natural Language" else "text"), inputs=target_lang_selection, outputs=target_code_output)
|
| 172 |
|