abdullah63 commited on
Commit
86dbc72
·
verified ·
1 Parent(s): 8b0d2ca

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -12
app.py CHANGED
@@ -140,7 +140,7 @@ sp_pseudo = spm.SentencePieceProcessor(model_file="pseudo.model") # For decodin
140
  sp_code = spm.SentencePieceProcessor(model_file="code.model") # For encoding C++ (source)
141
 
142
  # Load the full saved model (architecture + weights)
143
- model_path = "transformer_cpp_to_pseudo.pth" # Adjust path to your C++ to pseudocode model
144
  model = torch.load(model_path, map_location=device, weights_only=False)
145
  model.eval()
146
  model = model.to(device)
@@ -170,17 +170,26 @@ def respond(message, history, max_tokens):
170
  for response in generate_pseudocode(message, max_tokens):
171
  yield response
172
 
173
- # Gradio interface
174
- demo = gr.ChatInterface(
175
- respond,
176
- chatbot=gr.Chatbot(label="C++ to Pseudocode Generator"),
177
- textbox=gr.Textbox(placeholder="Enter C++ code (e.g., 'int x = 5; for(int i=0; i<x; i++) cout << i;')", label="C++ Code"),
178
- additional_inputs=[
179
- gr.Slider(minimum=10, maximum=1000, value=50, step=1, label="Max tokens"),
180
- ],
181
- title="C++ to Pseudocode Transformer",
182
- description="Convert C++ code to pseudocode using a custom transformer trained on the SPoC dataset.",
183
- )
 
 
 
 
 
 
 
 
 
184
 
185
  if __name__ == "__main__":
186
  demo.launch()
 
140
  sp_code = spm.SentencePieceProcessor(model_file="code.model") # For encoding C++ (source)
141
 
142
  # Load the full saved model (architecture + weights)
143
+ model_path = "transformer_cpp_to_pseudo.pth"
144
  model = torch.load(model_path, map_location=device, weights_only=False)
145
  model.eval()
146
  model = model.to(device)
 
170
  for response in generate_pseudocode(message, max_tokens):
171
  yield response
172
 
173
+ # Gradio UI setup with Blocks
174
+ with gr.Blocks(title="C++ to Pseudocode Transformer") as demo:
175
+ gr.Markdown("## C++ to Pseudocode Converter")
176
+ gr.Markdown("Enter C++ code below and press Submit to generate pseudocode.")
177
+ cpp_input = gr.Textbox(
178
+ label="C++ Code",
179
+ placeholder="e.g., 'int x = 5; for(int i=0; i<x; i++) cout << i;'",
180
+ lines=5
181
+ )
182
+ submit_btn = gr.Button("Submit", variant="primary")
183
+ pseudocode_output = gr.Textbox(
184
+ label="Generated Pseudocode",
185
+ lines=5
186
+ )
187
+
188
+ submit_btn.click(
189
+ fn=respond,
190
+ inputs=[cpp_input, gr.State(value=[]), gr.Slider(minimum=10, maximum=1000, value=50, step=1, visible=False)],
191
+ outputs=pseudocode_output
192
+ )
193
 
194
  if __name__ == "__main__":
195
  demo.launch()