Spaces:
Runtime error
Runtime error
Create App.py
Browse files
App.py
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
|
| 3 |
+
|
| 4 |
+
# Load the model and tokenizer
|
| 5 |
+
model_name = "facebook/codegen-350M-mono"
|
| 6 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 7 |
+
model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
|
| 8 |
+
|
| 9 |
+
# Function to translate Python code to JavaScript
|
| 10 |
+
def translate_code(python_code):
|
| 11 |
+
inputs = tokenizer(python_code, return_tensors="pt", max_length=512, truncation=True)
|
| 12 |
+
outputs = model.generate(**inputs, max_length=512, num_beams=5, early_stopping=True)
|
| 13 |
+
translated_code = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 14 |
+
return translated_code
|
| 15 |
+
|
| 16 |
+
# Gradio interface
|
| 17 |
+
interface = gr.Interface(
|
| 18 |
+
fn=translate_code,
|
| 19 |
+
inputs="text",
|
| 20 |
+
outputs="text",
|
| 21 |
+
title="Python to JavaScript Code Translator",
|
| 22 |
+
description="Paste your Python code, and it will be translated into JavaScript.",
|
| 23 |
+
)
|
| 24 |
+
|
| 25 |
+
# Launch the app
|
| 26 |
+
interface.launch()
|