TaruniSwathi commited on
Commit
bdc658e
Β·
verified Β·
1 Parent(s): 84d753c

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +231 -0
app.py ADDED
@@ -0,0 +1,231 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+
3
+ import gradio as gr
4
+ from huggingface_hub import hf_hub_download
5
+ from llama_cpp import Llama
6
+
7
+
8
+ MODEL_REPO = "TaruniSwathi/Qwen2.5-Coder-1.5B-Java-CSharp-GGUF"
9
+ MODEL_FILE = "Qwen2.5-Coder-1.5B-Java-CSharp_V2.Q4_K_M.gguf"
10
+
11
+
12
+ print("Downloading GGUF model...")
13
+
14
+ model_path = hf_hub_download(
15
+ repo_id=MODEL_REPO,
16
+ filename=MODEL_FILE,
17
+ )
18
+
19
+ print("Model downloaded:", model_path)
20
+ print("Loading model with llama.cpp...")
21
+
22
+
23
+ llm = Llama(
24
+ model_path=model_path,
25
+ n_ctx=2048,
26
+ n_threads=max(1, os.cpu_count() or 2),
27
+ n_batch=128,
28
+ n_gpu_layers=0,
29
+ verbose=False,
30
+ )
31
+
32
+ print("Model loaded successfully.")
33
+
34
+
35
+ def build_prompt(task: str, user_input: str) -> str:
36
+ user_input = user_input.strip()
37
+
38
+ if task == "Natural Language β†’ Java":
39
+ return f"""### Instruction:
40
+ {user_input}
41
+
42
+ ### Java Code:
43
+ """
44
+
45
+ return f"""### Instruction:
46
+ Translate the following Java code to C#.
47
+
48
+ ### Java Code:
49
+ {user_input}
50
+
51
+ ### C# Code:
52
+ """
53
+
54
+
55
+ def clean_output(task: str, generated_text: str) -> str:
56
+ generated_text = generated_text.strip()
57
+
58
+ stop_markers = [
59
+ "### Instruction:",
60
+ "### Java Code:",
61
+ "### C# Code:",
62
+ ]
63
+
64
+ for marker in stop_markers:
65
+ if marker in generated_text:
66
+ generated_text = generated_text.split(marker)[0].strip()
67
+
68
+ if task == "Natural Language β†’ Java":
69
+ language = "java"
70
+ else:
71
+ language = "csharp"
72
+
73
+ return f"```{language}\n{generated_text}\n```"
74
+
75
+
76
+ def generate_code(
77
+ task: str,
78
+ user_input: str,
79
+ max_tokens: int,
80
+ ) -> str:
81
+ if not user_input or not user_input.strip():
82
+ return "Please enter a description or Java code."
83
+
84
+ prompt = build_prompt(task, user_input)
85
+
86
+ try:
87
+ response = llm(
88
+ prompt,
89
+ max_tokens=int(max_tokens),
90
+ temperature=0.0,
91
+ top_p=1.0,
92
+ repeat_penalty=1.05,
93
+ stop=[
94
+ "### Instruction:",
95
+ "### Java Code:",
96
+ "### C# Code:",
97
+ ],
98
+ echo=False,
99
+ )
100
+
101
+ generated_text = response["choices"][0]["text"]
102
+
103
+ if not generated_text.strip():
104
+ return "The model returned an empty response. Please try a more detailed input."
105
+
106
+ return clean_output(task, generated_text)
107
+
108
+ except Exception as error:
109
+ return f"Generation failed: {error}"
110
+
111
+
112
+ def update_placeholder(task: str):
113
+ if task == "Natural Language β†’ Java":
114
+ return gr.update(
115
+ label="Natural-language requirement",
116
+ placeholder=(
117
+ "Example: Write a Java method to check whether "
118
+ "a number is prime."
119
+ ),
120
+ value="",
121
+ )
122
+
123
+ return gr.update(
124
+ label="Java code",
125
+ placeholder=(
126
+ "Example:\n"
127
+ "public static int factorial(int n) {\n"
128
+ " int result = 1;\n"
129
+ " for (int i = 2; i <= n; i++) result *= i;\n"
130
+ " return result;\n"
131
+ "}"
132
+ ),
133
+ value="",
134
+ )
135
+
136
+
137
+ with gr.Blocks(title="Java and C# CodeGen") as demo:
138
+ gr.Markdown(
139
+ """
140
+ # Java and C# CodeGen
141
+
142
+ Generate Java code from natural-language requirements or translate
143
+ Java code into C# using a fine-tuned Qwen2.5-Coder model.
144
+ """
145
+ )
146
+
147
+ task = gr.Dropdown(
148
+ choices=[
149
+ "Natural Language β†’ Java",
150
+ "Java β†’ C#",
151
+ ],
152
+ value="Natural Language β†’ Java",
153
+ label="Select task",
154
+ )
155
+
156
+ user_input = gr.Textbox(
157
+ label="Natural-language requirement",
158
+ placeholder=(
159
+ "Example: Write a Java method to check whether "
160
+ "a number is prime."
161
+ ),
162
+ lines=12,
163
+ )
164
+
165
+ max_tokens = gr.Slider(
166
+ minimum=64,
167
+ maximum=512,
168
+ value=256,
169
+ step=32,
170
+ label="Maximum generated tokens",
171
+ )
172
+
173
+ generate_button = gr.Button(
174
+ "Generate Code",
175
+ variant="primary",
176
+ )
177
+
178
+ output = gr.Markdown(
179
+ label="Generated code",
180
+ )
181
+
182
+ task.change(
183
+ fn=update_placeholder,
184
+ inputs=task,
185
+ outputs=user_input,
186
+ )
187
+
188
+ generate_button.click(
189
+ fn=generate_code,
190
+ inputs=[
191
+ task,
192
+ user_input,
193
+ max_tokens,
194
+ ],
195
+ outputs=output,
196
+ )
197
+
198
+ gr.Examples(
199
+ examples=[
200
+ [
201
+ "Natural Language β†’ Java",
202
+ "Write a Java method to calculate factorial of a number using a loop.",
203
+ 256,
204
+ ],
205
+ [
206
+ "Natural Language β†’ Java",
207
+ "Write a Java method to reverse a string.",
208
+ 256,
209
+ ],
210
+ [
211
+ "Java β†’ C#",
212
+ """public static int factorial(int n) {
213
+ int result = 1;
214
+ for (int i = 2; i <= n; i++) {
215
+ result *= i;
216
+ }
217
+ return result;
218
+ }""",
219
+ 256,
220
+ ],
221
+ ],
222
+ inputs=[
223
+ task,
224
+ user_input,
225
+ max_tokens,
226
+ ],
227
+ )
228
+
229
+
230
+ if __name__ == "__main__":
231
+ demo.queue(max_size=10).launch()