maulanayyy commited on
Commit
2cde64c
·
verified ·
1 Parent(s): 5152986

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -5
app.py CHANGED
@@ -1,6 +1,7 @@
1
  import gradio as gr
2
  import torch
3
  from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
 
4
 
5
  # Load the model and tokenizer
6
  model_name = "maulanayyy/codet5_code_translation-v3" # Ganti dengan nama model yang benar
@@ -11,7 +12,7 @@ model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
11
  device = "cuda" if torch.cuda.is_available() else "cpu"
12
  model.to(device) # Pindahkan model ke GPU jika tersedia
13
 
14
- # Function to perform inference
15
  def translate_code(input_code):
16
  try:
17
  # Prepare the input text
@@ -26,13 +27,18 @@ def translate_code(input_code):
26
 
27
  # Decode the output
28
  translated_code = tokenizer.decode(outputs[0], skip_special_tokens=True)
29
- return translated_code
 
 
 
 
 
30
  except Exception as e:
31
  print(f"Error during translation: {e}")
32
- return "An error occurred during translation."
33
 
34
- # Create Gradio interface
35
- demo = gr.Interface(fn=translate_code, inputs="text", outputs="text", title="Java to C# Code Translator", description="Enter Java code to translate it to C#.")
36
 
37
  # Launch the interface
38
  demo.launch(share=True)
 
1
  import gradio as gr
2
  import torch
3
  from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
4
+ import time # Untuk simulasi delay
5
 
6
  # Load the model and tokenizer
7
  model_name = "maulanayyy/codet5_code_translation-v3" # Ganti dengan nama model yang benar
 
12
  device = "cuda" if torch.cuda.is_available() else "cpu"
13
  model.to(device) # Pindahkan model ke GPU jika tersedia
14
 
15
+ # Function to perform inference with streaming
16
  def translate_code(input_code):
17
  try:
18
  # Prepare the input text
 
27
 
28
  # Decode the output
29
  translated_code = tokenizer.decode(outputs[0], skip_special_tokens=True)
30
+
31
+ # Simulate streaming output
32
+ for char in translated_code:
33
+ yield char # Mengirimkan karakter satu per satu
34
+ time.sleep(0.05) # Simulasi delay untuk streaming (dapat disesuaikan)
35
+
36
  except Exception as e:
37
  print(f"Error during translation: {e}")
38
+ yield "An error occurred during translation."
39
 
40
+ # Create Gradio interface with streaming
41
+ demo = gr.Interface(fn=translate_code, inputs="text", outputs="text", title="Java to C# Code Translator", description="Enter Java code to translate it to C#.", live=True)
42
 
43
  # Launch the interface
44
  demo.launch(share=True)