ramyaa1113 commited on
Commit
c3b34cd
Β·
1 Parent(s): 6c76fe5

app ui updated

Browse files
Files changed (1) hide show
  1. app.py +25 -38
app.py CHANGED
@@ -5,16 +5,11 @@ import tempfile
5
  from translator.agent import TranslationAgent
6
  from difflib import unified_diff
7
 
8
- # Initialize agent (will load mapping table etc.)
9
  agent = TranslationAgent()
10
 
11
- '''
12
- def do_translate(csharp_code, target_format, model_choice):
13
- # target_format: "cpp", "blueprint", "both"
14
- return agent.translate(csharp_code, target_format=target_format, model_choice=model_choice)
15
- '''
16
 
17
- # --- Save cpp files and BP Files ---
18
  def save_cpp_files(cpp_text: str):
19
  h_file = tempfile.NamedTemporaryFile(delete=False, suffix=".h")
20
  cpp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".cpp")
@@ -44,44 +39,37 @@ def show_diff(csharp: str, cpp: str) -> str:
44
  )
45
  return "\n".join(diff)
46
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
47
  def translate_with_mapping(csharp_code, target_format, model_choice, mapping_text):
48
  try:
49
  mapping = json.loads(mapping_text)
50
  except json.JSONDecodeError:
51
- return "⚠️ Invalid mapping JSON", "", "", ""
52
-
53
- # Update agent mapping
54
  agent.mapping = mapping
55
  cpp, blueprint = agent.translate(csharp_code, target_format=target_format, model_choice=model_choice)
56
- diff = show_diff(csharp_code, cpp)
57
  h_file, cpp_file = save_cpp_files(cpp)
58
- return h_file, cpp_file, blueprint, diff
59
-
60
- '''
61
- title = "Unity β†’ Unreal Code Translator"
62
- description = "Paste a Unity C# script and get suggested Unreal C++ (.h/.cpp) and an Unreal Python Blueprint-generator script."
63
-
64
- iface = gr.Interface(
65
- fn=do_translate,
66
- inputs=[
67
- gr.Textbox(lines=20, label="Unity C# code"),
68
- gr.Radio(["cpp", "blueprint", "both"], value="both", label="Target"),
69
- gr.Radio(["hf", "openai"], value="hf", label="Model backend")
70
- ],
71
- outputs=[
72
- gr.Textbox(lines=20, label="Unreal C++ (.h/.cpp)"),
73
- gr.Textbox(lines=20, label="Unreal Blueprint generator (Python for Editor)")
74
- ],
75
- title=title,
76
- description=description,
77
- flagging_mode="never",
78
- )
79
-
80
- '''
81
 
82
  # --- Gradio UI ---
83
  with gr.Blocks(title="Unity β†’ Unreal Code Translator") as demo:
84
- gr.Markdown("# 🧠 Unity β†’ Unreal Code Translator")
85
  gr.Markdown("Paste a Unity C# script and get suggested Unreal C++ (.h/.cpp) and a Blueprint Python generator.")
86
 
87
  with gr.Row():
@@ -94,7 +82,7 @@ with gr.Blocks(title="Unity β†’ Unreal Code Translator") as demo:
94
  cpp_file_out = gr.File(label=".cpp file")
95
  blueprint_out = gr.Code(label="Blueprint Python", language="python")
96
 
97
- diff_output = gr.Code(label="Preview Diff (C# β†’ C++)", language="diff")
98
 
99
  with gr.Accordion("Mapping Rules Playground", open=False):
100
  mapping_editor = gr.Textbox(
@@ -103,7 +91,7 @@ with gr.Blocks(title="Unity β†’ Unreal Code Translator") as demo:
103
  label="Edit Mapping JSON"
104
  )
105
 
106
- translate_btn = gr.Button("Translate πŸš€")
107
  translate_btn.click(
108
  translate_with_mapping,
109
  inputs=[csharp_box, target_format, backend, mapping_editor],
@@ -112,4 +100,3 @@ with gr.Blocks(title="Unity β†’ Unreal Code Translator") as demo:
112
 
113
  if __name__ == "__main__":
114
  demo.launch(server_name="0.0.0.0", server_port=7860, debug=True)
115
-
 
5
  from translator.agent import TranslationAgent
6
  from difflib import unified_diff
7
 
8
+ # Initialize agent
9
  agent = TranslationAgent()
10
 
 
 
 
 
 
11
 
12
+ # --- Helper functions ---
13
  def save_cpp_files(cpp_text: str):
14
  h_file = tempfile.NamedTemporaryFile(delete=False, suffix=".h")
15
  cpp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".cpp")
 
39
  )
40
  return "\n".join(diff)
41
 
42
+
43
+ def color_diff(diff_text: str) -> str:
44
+ """Wrap + lines in green, - lines in red for HTML preview"""
45
+ html = "<pre style='font-family: monospace;'>"
46
+ for line in diff_text.splitlines():
47
+ if line.startswith("+"):
48
+ html += f'<span style="color:green">{line}</span>\n'
49
+ elif line.startswith("-"):
50
+ html += f'<span style="color:red">{line}</span>\n'
51
+ else:
52
+ html += f"{line}\n"
53
+ html += "</pre>"
54
+ return html
55
+
56
+
57
  def translate_with_mapping(csharp_code, target_format, model_choice, mapping_text):
58
  try:
59
  mapping = json.loads(mapping_text)
60
  except json.JSONDecodeError:
61
+ return "Invalid mapping JSON", "", "", ""
62
+
 
63
  agent.mapping = mapping
64
  cpp, blueprint = agent.translate(csharp_code, target_format=target_format, model_choice=model_choice)
65
+ diff_html = color_diff(show_diff(csharp_code, cpp))
66
  h_file, cpp_file = save_cpp_files(cpp)
67
+ return h_file, cpp_file, blueprint, diff_html
68
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
69
 
70
  # --- Gradio UI ---
71
  with gr.Blocks(title="Unity β†’ Unreal Code Translator") as demo:
72
+ gr.Markdown("# Unity β†’ Unreal Code Translator")
73
  gr.Markdown("Paste a Unity C# script and get suggested Unreal C++ (.h/.cpp) and a Blueprint Python generator.")
74
 
75
  with gr.Row():
 
82
  cpp_file_out = gr.File(label=".cpp file")
83
  blueprint_out = gr.Code(label="Blueprint Python", language="python")
84
 
85
+ diff_output = gr.HTML(label="Preview Diff (C# β†’ C++)") # HTML for colored diff
86
 
87
  with gr.Accordion("Mapping Rules Playground", open=False):
88
  mapping_editor = gr.Textbox(
 
91
  label="Edit Mapping JSON"
92
  )
93
 
94
+ translate_btn = gr.Button("Translate")
95
  translate_btn.click(
96
  translate_with_mapping,
97
  inputs=[csharp_box, target_format, backend, mapping_editor],
 
100
 
101
  if __name__ == "__main__":
102
  demo.launch(server_name="0.0.0.0", server_port=7860, debug=True)