SoybeanMilk commited on
Commit
581b5dc
1 Parent(s): 146a1c2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -11
app.py CHANGED
@@ -57,16 +57,50 @@ def cp_text(input_text):
57
  def cp_clear():
58
  pyperclip.clear()
59
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
60
  # Add a translation function
61
- def translate(api_key, input_text, inputs_transStyle):
62
- genai.configure(api_key=api_key)
63
- model = genai.GenerativeModel('gemini-pro')
64
- if input_text is None or input_text == "":
65
- return "System prompt: There is no content to translate!"
66
-
67
- prompt = f"In the translation, ensure that no content is repeated and original meaning and context are preserved as much as possible. Reformat the following article to have clear paragraph breaks and correct punctuation. Then, translate it into {inputs_transStyle}, ensuring that the original meaning and context are preserved as much as possible."
68
- response = model.generate_content([prompt, input_text])
69
- return response.text
70
 
71
  def main():
72
 
@@ -100,7 +134,7 @@ def main():
100
  with gr.Row():
101
  # Create a text input box for users to enter their API key
102
  inputs_api_key = gr.Textbox(label="Please enter your API key here", type="password")
103
-
104
  with gr.Column():
105
  with gr.Row():
106
  outputs_text = gr.Textbox(label="Extract content", lines=20)
@@ -133,7 +167,7 @@ def main():
133
  clear_img_btn.click(fn=clear_content, inputs=[], outputs=[inputs_img])
134
 
135
  # ---------------------- 翻译 ----------------------
136
- translate_btn.click(fn=translate, inputs=[inputs_api_key, outputs_text, inputs_transStyle], outputs=[outputs_tr_text])
137
  clear_text_btn.click(fn=clear_content, inputs=[], outputs=[outputs_text])
138
 
139
  # ---------------------- 复制到剪贴板 ----------------------
 
57
  def cp_clear():
58
  pyperclip.clear()
59
 
60
+ # Split the text into 4000 character chunks
61
+ def process_text_input_text(input_text):
62
+ # Split the text into 4000 character chunks
63
+ chunks = [input_text[i:i+4000] for i in range(0, len(input_text), 4000)]
64
+ return chunks
65
+
66
+ def process_and_translate(api_key, input_text, inputs_transStyle):
67
+ # Process the input text into chunks
68
+ chunks = process_text_input_text(input_text)
69
+
70
+ # Translate each chunk and collect the results
71
+ translated_chunks = []
72
+ for chunk in chunks:
73
+ if chunk is None or chunk == "":
74
+ translated_chunks.append("System prompt: There is no content to translate!")
75
+ else:
76
+ prompt = f"Display language is {inputs_transStyle}, do not display original text, As a Knowledge Video Content Analysis Expert, I specialize in analyzing knowledge videos, identifying and clearly explaining key points in {inputs_transStyle}, ensuring accurate, easy-to-understand summaries suitable for diverse audiences, analyze, list key points, and explain detailedly below text: "
77
+ genai.configure(api_key=api_key)
78
+ model = genai.GenerativeModel('gemini-pro')
79
+ response = model.generate_content([prompt, chunk],
80
+ generation_config=genai.types.GenerationConfig(
81
+ # Only one candidate for now.
82
+ candidate_count=1,
83
+ stop_sequences=['ʤ'],
84
+ max_output_tokens=2048,
85
+ temperature=1.0)
86
+ )
87
+ translated_chunks.append(response.text)
88
+
89
+ # Join the translated chunks back together into a single string
90
+ response = '\n--------------------------------------------------\n'.join(translated_chunks)
91
+
92
+ return response
93
+
94
  # Add a translation function
95
+ # def translate(api_key, input_text, inputs_transStyle):
96
+ # genai.configure(api_key=api_key)
97
+ # model = genai.GenerativeModel('gemini-pro')
98
+ # if input_text is None or input_text == "":
99
+ # return "System prompt: There is no content to translate!"
100
+ #
101
+ # prompt = f"Via {inputs_transStyle}, As a Knowledge Video Content Analysis Expert, I specialize in analyzing knowledge videos, identifying and clearly explaining key points in English, ensuring accurate, easy-to-understand summaries suitable for diverse audiences."
102
+ # response = model.generate_content([prompt, input_text])
103
+ # return response.text
104
 
105
  def main():
106
 
 
134
  with gr.Row():
135
  # Create a text input box for users to enter their API key
136
  inputs_api_key = gr.Textbox(label="Please enter your API key here", type="password")
137
+
138
  with gr.Column():
139
  with gr.Row():
140
  outputs_text = gr.Textbox(label="Extract content", lines=20)
 
167
  clear_img_btn.click(fn=clear_content, inputs=[], outputs=[inputs_img])
168
 
169
  # ---------------------- 翻译 ----------------------
170
+ translate_btn.click(fn=process_and_translate, inputs=[inputs_api_key, outputs_text, inputs_transStyle], outputs=[outputs_tr_text])
171
  clear_text_btn.click(fn=clear_content, inputs=[], outputs=[outputs_text])
172
 
173
  # ---------------------- 复制到剪贴板 ----------------------