arinbalyan commited on
Commit
274a495
·
verified ·
1 Parent(s): 2ea1bf4

Upload app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +214 -0
app.py ADDED
@@ -0,0 +1,214 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import AutoModelForCausalLM, AutoTokenizer
3
+ import torch
4
+
5
+ MODEL_ID = "arinbalyan/code-translation-lora"
6
+
7
+ theme = (
8
+ gr.themes.Soft(primary_hue="indigo", neutral_hue="slate")
9
+ .set(button_primary_background_fill_hover="#4f46e5")
10
+ )
11
+
12
+ tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
13
+ model = AutoModelForCausalLM.from_pretrained(
14
+ MODEL_ID,
15
+ torch_dtype=torch.float16,
16
+ device_map="auto",
17
+ )
18
+
19
+ LANGUAGES = [
20
+ "Python", "JavaScript", "TypeScript", "Java", "Go",
21
+ "Rust", "C++", "Ruby", "PHP", "C#", "Swift", "Kotlin",
22
+ ]
23
+
24
+
25
+ def generate(
26
+ task: str,
27
+ source_lang: str,
28
+ target_lang: str,
29
+ source_code: str,
30
+ description: str,
31
+ ) -> str:
32
+ if task == "Code Translation":
33
+ if not source_code.strip():
34
+ return "# Enter source code to translate."
35
+ instruction = f"Translate the following {source_lang} code to {target_lang}:"
36
+ prompt = (
37
+ f"{instruction}\n\n"
38
+ f"Source code ({source_lang}):\n"
39
+ f"```{source_lang.lower()}\n"
40
+ f"{source_code.strip()}\n"
41
+ f"```\n\n"
42
+ f"Translated code ({target_lang}):\n"
43
+ f"```{target_lang.lower()}\n"
44
+ )
45
+ else:
46
+ if not description.strip():
47
+ return "# Describe what you want to code."
48
+ prompt = (
49
+ f"### Instruction:\n{description.strip()}\n\n"
50
+ f"### Code:\n"
51
+ )
52
+
53
+ inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
54
+ with torch.no_grad():
55
+ outputs = model.generate(
56
+ **inputs,
57
+ max_new_tokens=512,
58
+ temperature=0.3,
59
+ do_sample=True,
60
+ pad_token_id=tokenizer.eos_token_id,
61
+ )
62
+ result = tokenizer.decode(outputs[0], skip_special_tokens=True)
63
+ code = result[len(prompt):]
64
+ if code.endswith("```"):
65
+ code = code[:-3].strip()
66
+ return code.strip()
67
+
68
+
69
+ def update_visibility(task: str):
70
+ return {
71
+ translation_box: gr.update(visible=task == "Code Translation"),
72
+ translation_source_lang: gr.update(visible=task == "Code Translation"),
73
+ translation_target_lang: gr.update(visible=task == "Code Translation"),
74
+ translation_target_label: gr.update(visible=task == "Code Translation"),
75
+ generation_box: gr.update(visible=task == "Code Generation"),
76
+ generation_label: gr.update(visible=task == "Code Generation"),
77
+ }
78
+
79
+
80
+ with gr.Blocks(theme=theme, title="Code Translation") as demo:
81
+ gr.Markdown(
82
+ """
83
+ # 🔄 Code Translation Studio
84
+ Fine-tuned Qwen2.5-Coder-1.5B on code translation and generation tasks.
85
+ Translate code between languages or generate code from descriptions.
86
+ """
87
+ )
88
+
89
+ task = gr.Radio(
90
+ choices=["Code Translation", "Code Generation"],
91
+ value="Code Translation",
92
+ label="Task",
93
+ )
94
+
95
+ with gr.Row():
96
+ with gr.Column(scale=1):
97
+ translation_box = gr.Textbox(
98
+ label="Source Code",
99
+ placeholder="Paste your source code here...",
100
+ lines=10,
101
+ visible=True,
102
+ )
103
+ translation_source_lang = gr.Dropdown(
104
+ choices=LANGUAGES,
105
+ value="Python",
106
+ label="Source Language",
107
+ visible=True,
108
+ )
109
+ translation_target_lang = gr.Dropdown(
110
+ choices=LANGUAGES,
111
+ value="JavaScript",
112
+ label="Target Language",
113
+ visible=True,
114
+ )
115
+
116
+ with gr.Column(scale=1):
117
+ translation_target_label = gr.Code(
118
+ label="Translated Code",
119
+ language="javascript",
120
+ lines=10,
121
+ visible=True,
122
+ )
123
+
124
+ generation_box = gr.Textbox(
125
+ label="Description",
126
+ placeholder="e.g., Write a function to merge two sorted lists in Python...",
127
+ lines=4,
128
+ visible=False,
129
+ )
130
+ generation_label = gr.Code(
131
+ label="Generated Code",
132
+ language="python",
133
+ lines=10,
134
+ visible=False,
135
+ )
136
+
137
+ run_btn = gr.Button("Generate", variant="primary", size="lg")
138
+
139
+ gr.Examples(
140
+ examples=[
141
+ [
142
+ "Code Translation",
143
+ "def add(a, b):\n return a + b",
144
+ "Python",
145
+ "JavaScript",
146
+ "",
147
+ ],
148
+ [
149
+ "Code Translation",
150
+ "function isEven(n) { return n % 2 === 0; }",
151
+ "JavaScript",
152
+ "Python",
153
+ "",
154
+ ],
155
+ [
156
+ "Code Generation",
157
+ "",
158
+ "Python",
159
+ "JavaScript",
160
+ "Write a Python function to reverse a string.",
161
+ ],
162
+ [
163
+ "Code Generation",
164
+ "",
165
+ "Python",
166
+ "JavaScript",
167
+ "Write a function to check if a number is prime in Python.",
168
+ ],
169
+ ],
170
+ inputs=[
171
+ task,
172
+ translation_box,
173
+ translation_source_lang,
174
+ translation_target_lang,
175
+ generation_box,
176
+ ],
177
+ label="Try one of these",
178
+ )
179
+
180
+ task.change(
181
+ fn=update_visibility,
182
+ inputs=task,
183
+ outputs=[
184
+ translation_box,
185
+ translation_source_lang,
186
+ translation_target_lang,
187
+ translation_target_label,
188
+ generation_box,
189
+ generation_label,
190
+ ],
191
+ )
192
+
193
+ run_btn.click(
194
+ fn=generate,
195
+ inputs=[
196
+ task,
197
+ translation_source_lang,
198
+ translation_target_lang,
199
+ translation_box,
200
+ generation_box,
201
+ ],
202
+ outputs=[translation_target_label, generation_label],
203
+ )
204
+
205
+ gr.Markdown(
206
+ """
207
+ <div style="text-align:center; margin-top:1rem; color:gray; font-size:0.9rem;">
208
+ Base: Qwen2.5-Coder-1.5B &nbsp;•&nbsp; Fine-tune: LoRA (r=8) &nbsp;•&nbsp; Trained on Kaggle P100
209
+ </div>
210
+ """
211
+ )
212
+
213
+ if __name__ == "__main__":
214
+ demo.launch()