Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -247,6 +247,152 @@ model = AutoModelForCausalLM.from_pretrained(
|
|
| 247 |
trust_remote_code=True
|
| 248 |
)
|
| 249 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 250 |
@spaces.GPU
|
| 251 |
def process_validator_request(model_name, model_structure, template_instructions):
|
| 252 |
if not model_name or not model_structure:
|
|
@@ -265,7 +411,7 @@ def process_validator_request(model_name, model_structure, template_instructions
|
|
| 265 |
).to(model.device)
|
| 266 |
|
| 267 |
with torch.no_grad():
|
| 268 |
-
|
| 269 |
input_ids,
|
| 270 |
max_new_tokens=4096,
|
| 271 |
pad_token_id=tokenizer.eos_token_id,
|
|
@@ -279,9 +425,9 @@ def process_validator_request(model_name, model_structure, template_instructions
|
|
| 279 |
|
| 280 |
response = response.strip()
|
| 281 |
if response.startswith("```csharp"):
|
| 282 |
-
|
| 283 |
if response.endswith("```"):
|
| 284 |
-
|
| 285 |
|
| 286 |
return response
|
| 287 |
|
|
@@ -289,34 +435,6 @@ def process_validator_request(model_name, model_structure, template_instructions
|
|
| 289 |
print(f"An error occurred: {e}")
|
| 290 |
return f"An error occurred during generation: {e}"
|
| 291 |
|
| 292 |
-
# with gr.Blocks(title="C# Validator Generator") as demo:
|
| 293 |
-
# gr.Markdown("""
|
| 294 |
-
# # C# Validator Code Generator
|
| 295 |
-
# Use this tool to generate a C# validator class based on your model structure and template instructions,
|
| 296 |
-
# following a specific architectural pattern.
|
| 297 |
-
# """)
|
| 298 |
-
|
| 299 |
-
# with gr.Row():
|
| 300 |
-
# with gr.Column():
|
| 301 |
-
# model_name_input = gr.Textbox(label="Model Name (e.g., Role)", placeholder="Enter the model name (must be a valid C# identifier)...", interactive=True)
|
| 302 |
-
# model_structure_input = gr.Textbox(label="Model Structure (C# Class Definition)", placeholder="Paste your C# class definition here (including public properties)...", lines=15, interactive=True)
|
| 303 |
-
# template_instructions_input = gr.Textbox(label="Specific Template Instructions (Optional)", placeholder="Add any specific validation rules (e.g., 'Validate Id as GUID', 'Check if list is not null and not empty')...", lines=5, interactive=True)
|
| 304 |
-
# generate_button = gr.Button("Generate Validator Code")
|
| 305 |
-
|
| 306 |
-
# with gr.Column():
|
| 307 |
-
# code_output = gr.Code(label="Generated C# Validator Code",lines=25, interactive=False)
|
| 308 |
-
|
| 309 |
-
# generate_button.click(
|
| 310 |
-
# fn=process_validator_request,
|
| 311 |
-
# inputs=[model_name_input, model_structure_input, template_instructions_input],
|
| 312 |
-
# outputs=[code_output]
|
| 313 |
-
# )
|
| 314 |
-
|
| 315 |
-
|
| 316 |
-
# demo.launch()
|
| 317 |
-
|
| 318 |
-
import gradio as gr
|
| 319 |
-
|
| 320 |
@spaces.GPU
|
| 321 |
def process_multiple_validators(model_names_str, model_structure, template_instructions):
|
| 322 |
model_names = [name.strip() for name in model_names_str.split(",") if name.strip()]
|
|
@@ -328,27 +446,23 @@ def process_multiple_validators(model_names_str, model_structure, template_instr
|
|
| 328 |
|
| 329 |
return files_output
|
| 330 |
|
| 331 |
-
@spaces.GPU
|
| 332 |
-
|
| 333 |
def render_outputs(model_names_str, model_structure, template_instructions):
|
| 334 |
files = process_multiple_validators(model_names_str, model_structure, template_instructions)
|
| 335 |
components = []
|
| 336 |
|
| 337 |
for name, code in files.items():
|
| 338 |
-
|
| 339 |
-
|
| 340 |
-
|
| 341 |
-
|
| 342 |
-
)
|
| 343 |
|
| 344 |
return components
|
| 345 |
|
| 346 |
-
|
| 347 |
-
|
| 348 |
-
gr.Markdown("## 🧠 C# Validator Code Generator")
|
| 349 |
|
| 350 |
with gr.Tabs():
|
| 351 |
-
#
|
| 352 |
with gr.Tab("Single Validator"):
|
| 353 |
with gr.Row():
|
| 354 |
with gr.Column():
|
|
@@ -358,7 +472,7 @@ with gr.Blocks(title="C# Validator Generator") as demo:
|
|
| 358 |
generate_btn = gr.Button("Generate Validator")
|
| 359 |
|
| 360 |
with gr.Column():
|
| 361 |
-
single_code_output = gr.Code(label="Generated Code", lines=25)
|
| 362 |
|
| 363 |
generate_btn.click(
|
| 364 |
fn=process_validator_request,
|
|
@@ -366,7 +480,7 @@ with gr.Blocks(title="C# Validator Generator") as demo:
|
|
| 366 |
outputs=[single_code_output],
|
| 367 |
)
|
| 368 |
|
| 369 |
-
#
|
| 370 |
with gr.Tab("Batch Validators"):
|
| 371 |
with gr.Row():
|
| 372 |
with gr.Column():
|
|
@@ -387,7 +501,11 @@ with gr.Blocks(title="C# Validator Generator") as demo:
|
|
| 387 |
outputs=[dynamic_outputs],
|
| 388 |
)
|
| 389 |
|
| 390 |
-
|
|
|
|
|
|
|
|
|
|
| 391 |
|
|
|
|
| 392 |
|
| 393 |
|
|
|
|
| 247 |
trust_remote_code=True
|
| 248 |
)
|
| 249 |
|
| 250 |
+
# @spaces.GPU
|
| 251 |
+
# def process_validator_request(model_name, model_structure, template_instructions):
|
| 252 |
+
# if not model_name or not model_structure:
|
| 253 |
+
# return "Error: Model Name and Model Structure are required."
|
| 254 |
+
|
| 255 |
+
# try:
|
| 256 |
+
# generated_prompt = generate_validator_prompt(model_name, model_structure, template_instructions)
|
| 257 |
+
|
| 258 |
+
# messages = [{"role": "user", "content": generated_prompt}]
|
| 259 |
+
|
| 260 |
+
# input_ids = tokenizer.apply_chat_template(
|
| 261 |
+
# messages,
|
| 262 |
+
# tokenize=True,
|
| 263 |
+
# return_tensors="pt",
|
| 264 |
+
# add_generation_prompt=True,
|
| 265 |
+
# ).to(model.device)
|
| 266 |
+
|
| 267 |
+
# with torch.no_grad():
|
| 268 |
+
# outputs = model.generate(
|
| 269 |
+
# input_ids,
|
| 270 |
+
# max_new_tokens=4096,
|
| 271 |
+
# pad_token_id=tokenizer.eos_token_id,
|
| 272 |
+
# eos_token_id=tokenizer.eos_token_id,
|
| 273 |
+
# do_sample=True,
|
| 274 |
+
# temperature=0.7,
|
| 275 |
+
# top_p=0.95,
|
| 276 |
+
# )
|
| 277 |
+
|
| 278 |
+
# response = tokenizer.decode(outputs[0][input_ids.shape[-1]:], skip_special_tokens=True)
|
| 279 |
+
|
| 280 |
+
# response = response.strip()
|
| 281 |
+
# if response.startswith("```csharp"):
|
| 282 |
+
# response = response[len("```csharp"):].strip()
|
| 283 |
+
# if response.endswith("```"):
|
| 284 |
+
# response = response[:-len("```")].strip()
|
| 285 |
+
|
| 286 |
+
# return response
|
| 287 |
+
|
| 288 |
+
# except Exception as e:
|
| 289 |
+
# print(f"An error occurred: {e}")
|
| 290 |
+
# return f"An error occurred during generation: {e}"
|
| 291 |
+
|
| 292 |
+
# # with gr.Blocks(title="C# Validator Generator") as demo:
|
| 293 |
+
# # gr.Markdown("""
|
| 294 |
+
# # # C# Validator Code Generator
|
| 295 |
+
# # Use this tool to generate a C# validator class based on your model structure and template instructions,
|
| 296 |
+
# # following a specific architectural pattern.
|
| 297 |
+
# # """)
|
| 298 |
+
|
| 299 |
+
# # with gr.Row():
|
| 300 |
+
# # with gr.Column():
|
| 301 |
+
# # model_name_input = gr.Textbox(label="Model Name (e.g., Role)", placeholder="Enter the model name (must be a valid C# identifier)...", interactive=True)
|
| 302 |
+
# # model_structure_input = gr.Textbox(label="Model Structure (C# Class Definition)", placeholder="Paste your C# class definition here (including public properties)...", lines=15, interactive=True)
|
| 303 |
+
# # template_instructions_input = gr.Textbox(label="Specific Template Instructions (Optional)", placeholder="Add any specific validation rules (e.g., 'Validate Id as GUID', 'Check if list is not null and not empty')...", lines=5, interactive=True)
|
| 304 |
+
# # generate_button = gr.Button("Generate Validator Code")
|
| 305 |
+
|
| 306 |
+
# # with gr.Column():
|
| 307 |
+
# # code_output = gr.Code(label="Generated C# Validator Code",lines=25, interactive=False)
|
| 308 |
+
|
| 309 |
+
# # generate_button.click(
|
| 310 |
+
# # fn=process_validator_request,
|
| 311 |
+
# # inputs=[model_name_input, model_structure_input, template_instructions_input],
|
| 312 |
+
# # outputs=[code_output]
|
| 313 |
+
# # )
|
| 314 |
+
|
| 315 |
+
|
| 316 |
+
# # demo.launch()
|
| 317 |
+
|
| 318 |
+
# import gradio as gr
|
| 319 |
+
|
| 320 |
+
# @spaces.GPU
|
| 321 |
+
# def process_multiple_validators(model_names_str, model_structure, template_instructions):
|
| 322 |
+
# model_names = [name.strip() for name in model_names_str.split(",") if name.strip()]
|
| 323 |
+
# files_output = {}
|
| 324 |
+
|
| 325 |
+
# for model_name in model_names:
|
| 326 |
+
# result = process_validator_request(model_name, model_structure, template_instructions)
|
| 327 |
+
# files_output[model_name] = result
|
| 328 |
+
|
| 329 |
+
# return files_output
|
| 330 |
+
|
| 331 |
+
# @spaces.GPU
|
| 332 |
+
|
| 333 |
+
# def render_outputs(model_names_str, model_structure, template_instructions):
|
| 334 |
+
# files = process_multiple_validators(model_names_str, model_structure, template_instructions)
|
| 335 |
+
# components = []
|
| 336 |
+
|
| 337 |
+
# for name, code in files.items():
|
| 338 |
+
# components.append(
|
| 339 |
+
# gr.Accordion(f"{name}.Validator.cs", open=False).render()(
|
| 340 |
+
# gr.Code(value=code, lines=20, label=name)
|
| 341 |
+
# )
|
| 342 |
+
# )
|
| 343 |
+
|
| 344 |
+
# return components
|
| 345 |
+
|
| 346 |
+
|
| 347 |
+
# with gr.Blocks(title="C# Validator Generator") as demo:
|
| 348 |
+
# gr.Markdown("## 🧠 C# Validator Code Generator")
|
| 349 |
+
|
| 350 |
+
# with gr.Tabs():
|
| 351 |
+
# # ✅ Tab 1: Single
|
| 352 |
+
# with gr.Tab("Single Validator"):
|
| 353 |
+
# with gr.Row():
|
| 354 |
+
# with gr.Column():
|
| 355 |
+
# model_name_input = gr.Textbox(label="Model Name")
|
| 356 |
+
# model_structure_input = gr.Textbox(label="Model Structure", lines=15)
|
| 357 |
+
# template_input = gr.Textbox(label="Template Instructions (Optional)", lines=4)
|
| 358 |
+
# generate_btn = gr.Button("Generate Validator")
|
| 359 |
+
|
| 360 |
+
# with gr.Column():
|
| 361 |
+
# single_code_output = gr.Code(label="Generated Code", lines=25)
|
| 362 |
+
|
| 363 |
+
# generate_btn.click(
|
| 364 |
+
# fn=process_validator_request,
|
| 365 |
+
# inputs=[model_name_input, model_structure_input, template_input],
|
| 366 |
+
# outputs=[single_code_output],
|
| 367 |
+
# )
|
| 368 |
+
|
| 369 |
+
# # ✅ Tab 2: Batch
|
| 370 |
+
# with gr.Tab("Batch Validators"):
|
| 371 |
+
# with gr.Row():
|
| 372 |
+
# with gr.Column():
|
| 373 |
+
# model_names_input = gr.Textbox(
|
| 374 |
+
# label="Model Names (comma-separated)",
|
| 375 |
+
# placeholder="e.g., User, Role, Customer"
|
| 376 |
+
# )
|
| 377 |
+
# shared_model_structure_input = gr.Textbox(label="Shared Model Structure", lines=10)
|
| 378 |
+
# shared_template_input = gr.Textbox(label="Shared Template Instructions (Optional)", lines=3)
|
| 379 |
+
# batch_generate_btn = gr.Button("Generate All")
|
| 380 |
+
|
| 381 |
+
# with gr.Column():
|
| 382 |
+
# dynamic_outputs = gr.Column()
|
| 383 |
+
|
| 384 |
+
# batch_generate_btn.click(
|
| 385 |
+
# fn=render_outputs,
|
| 386 |
+
# inputs=[model_names_input, shared_model_structure_input, shared_template_input],
|
| 387 |
+
# outputs=[dynamic_outputs],
|
| 388 |
+
# )
|
| 389 |
+
|
| 390 |
+
# demo.launch()
|
| 391 |
+
|
| 392 |
+
import gradio as gr
|
| 393 |
+
import torch
|
| 394 |
+
|
| 395 |
+
# افتراضياً لديك هذه الدالة كما كتبتها
|
| 396 |
@spaces.GPU
|
| 397 |
def process_validator_request(model_name, model_structure, template_instructions):
|
| 398 |
if not model_name or not model_structure:
|
|
|
|
| 411 |
).to(model.device)
|
| 412 |
|
| 413 |
with torch.no_grad():
|
| 414 |
+
outputs = model.generate(
|
| 415 |
input_ids,
|
| 416 |
max_new_tokens=4096,
|
| 417 |
pad_token_id=tokenizer.eos_token_id,
|
|
|
|
| 425 |
|
| 426 |
response = response.strip()
|
| 427 |
if response.startswith("```csharp"):
|
| 428 |
+
response = response[len("```csharp"):].strip()
|
| 429 |
if response.endswith("```"):
|
| 430 |
+
response = response[:-len("```")].strip()
|
| 431 |
|
| 432 |
return response
|
| 433 |
|
|
|
|
| 435 |
print(f"An error occurred: {e}")
|
| 436 |
return f"An error occurred during generation: {e}"
|
| 437 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 438 |
@spaces.GPU
|
| 439 |
def process_multiple_validators(model_names_str, model_structure, template_instructions):
|
| 440 |
model_names = [name.strip() for name in model_names_str.split(",") if name.strip()]
|
|
|
|
| 446 |
|
| 447 |
return files_output
|
| 448 |
|
|
|
|
|
|
|
| 449 |
def render_outputs(model_names_str, model_structure, template_instructions):
|
| 450 |
files = process_multiple_validators(model_names_str, model_structure, template_instructions)
|
| 451 |
components = []
|
| 452 |
|
| 453 |
for name, code in files.items():
|
| 454 |
+
# إنشاء أكورديون لكل نتيجة
|
| 455 |
+
accordion = gr.Accordion(f"{name}.Validator.cs", open=False)
|
| 456 |
+
code_component = gr.Code(value=code, lines=20, label=name)
|
| 457 |
+
components.append(accordion(code_component))
|
|
|
|
| 458 |
|
| 459 |
return components
|
| 460 |
|
| 461 |
+
with gr.Blocks(title="C# Validator Generator with 8 Tabs") as demo:
|
| 462 |
+
gr.Markdown("# C# Validator & Entity Generator")
|
|
|
|
| 463 |
|
| 464 |
with gr.Tabs():
|
| 465 |
+
# Tab 1: Single Validator
|
| 466 |
with gr.Tab("Single Validator"):
|
| 467 |
with gr.Row():
|
| 468 |
with gr.Column():
|
|
|
|
| 472 |
generate_btn = gr.Button("Generate Validator")
|
| 473 |
|
| 474 |
with gr.Column():
|
| 475 |
+
single_code_output = gr.Code(label="Generated Validator Code", lines=25)
|
| 476 |
|
| 477 |
generate_btn.click(
|
| 478 |
fn=process_validator_request,
|
|
|
|
| 480 |
outputs=[single_code_output],
|
| 481 |
)
|
| 482 |
|
| 483 |
+
# Tab 2: Batch Validators
|
| 484 |
with gr.Tab("Batch Validators"):
|
| 485 |
with gr.Row():
|
| 486 |
with gr.Column():
|
|
|
|
| 501 |
outputs=[dynamic_outputs],
|
| 502 |
)
|
| 503 |
|
| 504 |
+
# Tab 3 to Tab 8 placeholders for future features
|
| 505 |
+
for i in range(3, 9):
|
| 506 |
+
with gr.Tab(f"Tab {i} - Placeholder"):
|
| 507 |
+
gr.Markdown(f"Content for Tab {i} goes here...")
|
| 508 |
|
| 509 |
+
demo.launch()
|
| 510 |
|
| 511 |
|