Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,58 +1,54 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from transformers import
|
| 3 |
import torch
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
return {fashion_items[i]: float(text_probs[0, i]) for i in range(len(fashion_items))}
|
| 47 |
|
| 48 |
# Gradio interface
|
| 49 |
-
|
| 50 |
-
fn=
|
| 51 |
-
inputs=gr.Textbox(
|
| 52 |
-
outputs=
|
| 53 |
-
title="
|
| 54 |
-
|
| 55 |
-
)
|
| 56 |
-
|
| 57 |
-
# Launch the interface
|
| 58 |
-
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 3 |
import torch
|
| 4 |
+
|
| 5 |
+
# Load model (CPU-friendly, no token required)
|
| 6 |
+
model_id = "replit/replit-code-v1_5-3b"
|
| 7 |
+
|
| 8 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 9 |
+
model = AutoModelForCausalLM.from_pretrained(model_id)
|
| 10 |
+
|
| 11 |
+
# Ensure it's on CPU
|
| 12 |
+
device = torch.device("cpu")
|
| 13 |
+
model.to(device)
|
| 14 |
+
|
| 15 |
+
def convert_python_to_r(python_code):
|
| 16 |
+
# Prompt to guide the model
|
| 17 |
+
prompt = f"""### Task:
|
| 18 |
+
Convert the following Python code to equivalent R code.
|
| 19 |
+
|
| 20 |
+
### Python code:
|
| 21 |
+
{python_code}
|
| 22 |
+
|
| 23 |
+
### R code:
|
| 24 |
+
"""
|
| 25 |
+
|
| 26 |
+
# Tokenize input
|
| 27 |
+
input_ids = tokenizer(prompt, return_tensors="pt", truncation=True).input_ids.to(device)
|
| 28 |
+
|
| 29 |
+
# Generate
|
| 30 |
+
outputs = model.generate(
|
| 31 |
+
input_ids,
|
| 32 |
+
max_length=512,
|
| 33 |
+
temperature=0.2,
|
| 34 |
+
do_sample=True,
|
| 35 |
+
pad_token_id=tokenizer.eos_token_id
|
| 36 |
+
)
|
| 37 |
+
|
| 38 |
+
# Decode result
|
| 39 |
+
result = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 40 |
+
|
| 41 |
+
# Extract R code from the result (after prompt)
|
| 42 |
+
if "### R code:" in result:
|
| 43 |
+
result = result.split("### R code:")[-1].strip()
|
| 44 |
+
|
| 45 |
+
return result
|
|
|
|
| 46 |
|
| 47 |
# Gradio interface
|
| 48 |
+
gr.Interface(
|
| 49 |
+
fn=convert_python_to_r,
|
| 50 |
+
inputs=gr.Textbox(lines=10, placeholder="Paste your Python code here..."),
|
| 51 |
+
outputs="text",
|
| 52 |
+
title="Python to R Code Converter",
|
| 53 |
+
description="Converts Python code to R using Replit Code Model (3B). Optimized for Hugging Face CPU Basic tier."
|
| 54 |
+
).launch()
|
|
|
|
|
|
|
|
|