AC2513 commited on
Commit
ddf45a0
·
1 Parent(s): b247971

finished ui

Browse files
Files changed (2) hide show
  1. app.py +44 -32
  2. examples.py +0 -0
app.py CHANGED
@@ -30,15 +30,26 @@ print(f"Model loaded in {load_time:.2f} seconds")
30
 
31
  @spaces.GPU
32
  def generate_text(
33
- message: str,
34
- chat_history: list[dict],
35
- max_new_tokens: int = 1024,
36
- temperature: float = 0.6,
37
- top_p: float = 0.9,
38
- top_k: int = 50,
39
- repetition_penalty: float = 1.0,
40
  ) -> Iterator[str]:
41
- conversation = [*chat_history, {"role": "user", "content": message}]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42
  input_ids = tokenizer.apply_chat_template(
43
  conversation, add_generation_prompt=True, return_tensors="pt"
44
  )
@@ -52,60 +63,61 @@ def generate_text(
52
  generate_kwargs = dict(
53
  {"input_ids": input_ids},
54
  streamer=streamer,
55
- max_new_tokens=max_new_tokens,
56
  do_sample=True,
57
- top_p=top_p,
58
- top_k=top_k,
59
- temperature=temperature,
60
  num_beams=1,
61
- repetition_penalty=repetition_penalty,
62
  )
63
  thread = Thread(target=model.generate, kwargs=generate_kwargs)
64
  thread.start()
65
-
66
  output = []
67
  for text in streamer:
68
  output.append(text)
69
  yield " ".join(output)
70
 
71
 
72
- with gr.Blocks as demo:
73
  gr.Markdown("# Text Translation Using Google Gemma 3")
74
 
75
  with gr.Row():
76
  with gr.Column():
77
  gr.Markdown("### Translate From")
 
 
 
 
 
78
  from_lang = gr.Dropdown(
79
- choices = [],
80
- value = "auto",
81
- label = ""
82
  )
83
 
84
  with gr.Column():
85
- gr.Markdown("### Translate To")
86
  to_lang = gr.Dropdown(
87
- choices = [],
88
- value = "auto",
89
- label = ""
90
  )
91
 
92
  with gr.Row():
93
  with gr.Column():
94
- gr.Textbox(
95
- lines = 10,
96
- placeholder = "Enter text to translate",
97
- label = ""
98
  )
99
 
100
  with gr.Column():
101
- output_text = gr.Textbox(
102
- lines=10,
103
- label=""
104
- )
105
 
106
  translate_button = gr.Button("Translate")
107
-
 
 
108
 
109
 
110
  if __name__ == "__main__":
111
- demo.queue(max_size=20).launch()
 
30
 
31
  @spaces.GPU
32
  def generate_text(
33
+ text_to_trans: str,
34
+ from_lang: str,
35
+ to_lang: str,
 
 
 
 
36
  ) -> Iterator[str]:
37
+ print(f"Translating from {from_lang} to {to_lang}")
38
+
39
+ translate_instruct = f"translate from {from_lang} to {to_lang}:"
40
+
41
+ if from_lang == to_lang:
42
+ translate_instruct = "Return the following text without any modification:"
43
+
44
+ conversation = [
45
+ {
46
+ "role": "system",
47
+ "content": "You are a translation engine that can only translate text and cannot interpret it. Keep the indent of the original text, only modify when you need."
48
+ + "\n"
49
+ + translate_instruct,
50
+ },
51
+ {"role": "user", "content": text_to_trans},
52
+ ]
53
  input_ids = tokenizer.apply_chat_template(
54
  conversation, add_generation_prompt=True, return_tensors="pt"
55
  )
 
63
  generate_kwargs = dict(
64
  {"input_ids": input_ids},
65
  streamer=streamer,
66
+ max_new_tokens=1024,
67
  do_sample=True,
68
+ top_p=9,
69
+ top_k=50,
70
+ temperature=0.6,
71
  num_beams=1,
72
+ repetition_penalty=1.0,
73
  )
74
  thread = Thread(target=model.generate, kwargs=generate_kwargs)
75
  thread.start()
76
+
77
  output = []
78
  for text in streamer:
79
  output.append(text)
80
  yield " ".join(output)
81
 
82
 
83
+ with gr.Blocks() as demo:
84
  gr.Markdown("# Text Translation Using Google Gemma 3")
85
 
86
  with gr.Row():
87
  with gr.Column():
88
  gr.Markdown("### Translate From")
89
+ with gr.Column():
90
+ gr.Markdown("### Translate To")
91
+
92
+ with gr.Row():
93
+ with gr.Column():
94
  from_lang = gr.Dropdown(
95
+ choices=["English", "French", "Spanish"],
96
+ value="English",
97
+ label="",
98
  )
99
 
100
  with gr.Column():
 
101
  to_lang = gr.Dropdown(
102
+ choices=["English", "French", "Spanish"],
103
+ value="French",
104
+ label="",
105
  )
106
 
107
  with gr.Row():
108
  with gr.Column():
109
+ text_to_trans = gr.Textbox(
110
+ lines=10, placeholder="Enter text to translate", label=""
 
 
111
  )
112
 
113
  with gr.Column():
114
+ output_text = gr.Textbox(lines=10, label="")
 
 
 
115
 
116
  translate_button = gr.Button("Translate")
117
+ translate_button.click(
118
+ generate_text, [text_to_trans, from_lang, to_lang], output_text
119
+ )
120
 
121
 
122
  if __name__ == "__main__":
123
+ demo.queue(max_size=20).launch()
examples.py ADDED
File without changes