SoybeanMilk commited on
Commit
aa0d25a
1 Parent(s): 5f1dbf2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +69 -22
app.py CHANGED
@@ -57,10 +57,10 @@ def cp_text(input_text):
57
  def cp_clear():
58
  pyperclip.clear()
59
 
60
- # Split the text into 1500 character chunks
61
  def process_text_input_text(input_text):
62
- # Split the text into 1500 character chunks
63
- chunks = [input_text[i:i+1500] for i in range(0, len(input_text), 1500)]
64
  return chunks
65
 
66
  def process_and_translate(api_key, input_text, src_lang, tgt_lang):
@@ -73,22 +73,46 @@ def process_and_translate(api_key, input_text, src_lang, tgt_lang):
73
  if chunk is None or chunk == "":
74
  translated_chunks.append("System prompt: There is no content to translate!")
75
  else:
76
- prompt = f"This is an {src_lang} to {tgt_lang} translation, please provide the {tgt_lang} translation for this sentence. Do not provide any explanations or text apart from the translation.\n{src_lang}: "
 
 
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
- max_output_tokens=2048,
84
- temperature=0.3,
85
- top_p=1,
86
- )
87
  )
88
  translated_chunks.append(response.text)
89
 
90
  # Join the translated chunks back together into a single string
91
- response = ''.join(translated_chunks)
92
 
93
  return response
94
 
@@ -104,20 +128,42 @@ def process_and_summary(api_key, input_text, src_lang, tgt_lang):
104
  else:
105
  prompt = f"This is an {src_lang} to {tgt_lang} summarization and knowledge key points, please provide the {tgt_lang} summarization and list the {tgt_lang} knowledge key points for this sentence. Do not provide any explanations or text apart from the summarization.\n{src_lang}: "
106
  genai.configure(api_key=api_key)
107
- model = genai.GenerativeModel('gemini-pro')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
108
  response = model.generate_content([prompt, chunk],
109
- generation_config=genai.types.GenerationConfig(
110
- # Only one candidate for now.
111
- candidate_count=1,
112
- max_output_tokens=2048,
113
- temperature=0.3,
114
- top_p=1,
115
- )
116
  )
117
  translated_chunks.append(response.text)
118
 
119
  # Join the translated chunks back together into a single string
120
- response = '\n==================================================\n'.join(translated_chunks)
121
 
122
  return response
123
 
@@ -199,6 +245,7 @@ def main():
199
  # ---------------------- 复制到剪贴板 ----------------------
200
  cp_btn.click(fn=cp_text, inputs=[outputs_tr_text], outputs=[])
201
  cp_clear_btn.click(fn=cp_clear, inputs=[], outputs=[])
 
202
 
203
  ocr_tr.launch(inbrowser=True)
204
 
 
57
  def cp_clear():
58
  pyperclip.clear()
59
 
60
+ # Split the text into 2000 character chunks
61
  def process_text_input_text(input_text):
62
+ # Split the text into 2000 character chunks
63
+ chunks = [input_text[i:i+2000] for i in range(0, len(input_text), 2000)]
64
  return chunks
65
 
66
  def process_and_translate(api_key, input_text, src_lang, tgt_lang):
 
73
  if chunk is None or chunk == "":
74
  translated_chunks.append("System prompt: There is no content to translate!")
75
  else:
76
+ prompt = f"This is an {src_lang} to {tgt_lang} translation, please provide the {tgt_lang} translation for this paragraph. Do not provide any explanations or text apart from the translation.\n{src_lang}: "
77
+ #prompt = f"This is an {src_lang} to {tgt_lang} translation, please provide the {tgt_lang} translation for this sentence. Do not provide any explanations or text apart from the translation.\n{src_lang}: "
78
+
79
  genai.configure(api_key=api_key)
80
+
81
+ generation_config = {
82
+ "candidateCount": 1,
83
+ "maxOutputTokens": 2048,
84
+ "temperature": 0.3,
85
+ "topP": 1
86
+ }
87
+
88
+ safety_settings = [
89
+ {
90
+ "category": "HARM_CATEGORY_HARASSMENT",
91
+ "threshold": "BLOCK_NONE",
92
+ },
93
+ {
94
+ "category": "HARM_CATEGORY_HATE_SPEECH",
95
+ "threshold": "BLOCK_NONE",
96
+ },
97
+ {
98
+ "category": "HARM_CATEGORY_SEXUALLY_EXPLICIT",
99
+ "threshold": "BLOCK_NONE",
100
+ },
101
+ {
102
+ "category": "HARM_CATEGORY_DANGEROUS_CONTENT",
103
+ "threshold": "BLOCK_NONE",
104
+ },
105
+ ]
106
+
107
+ model = genai.GenerativeModel(model_name='gemini-pro')
108
  response = model.generate_content([prompt, chunk],
109
+ #generation_config=generation_config,
110
+ safety_settings=safety_settings
 
 
 
 
 
111
  )
112
  translated_chunks.append(response.text)
113
 
114
  # Join the translated chunks back together into a single string
115
+ response = '\n\n'.join(translated_chunks)
116
 
117
  return response
118
 
 
128
  else:
129
  prompt = f"This is an {src_lang} to {tgt_lang} summarization and knowledge key points, please provide the {tgt_lang} summarization and list the {tgt_lang} knowledge key points for this sentence. Do not provide any explanations or text apart from the summarization.\n{src_lang}: "
130
  genai.configure(api_key=api_key)
131
+
132
+ generation_config = {
133
+ "candidateCount": 1,
134
+ "maxOutputTokens": 2048,
135
+ "temperature": 0.3,
136
+ "topP": 1
137
+ }
138
+
139
+ safety_settings = [
140
+ {
141
+ "category": "HARM_CATEGORY_HARASSMENT",
142
+ "threshold": "BLOCK_NONE",
143
+ },
144
+ {
145
+ "category": "HARM_CATEGORY_HATE_SPEECH",
146
+ "threshold": "BLOCK_NONE",
147
+ },
148
+ {
149
+ "category": "HARM_CATEGORY_SEXUALLY_EXPLICIT",
150
+ "threshold": "BLOCK_NONE",
151
+ },
152
+ {
153
+ "category": "HARM_CATEGORY_DANGEROUS_CONTENT",
154
+ "threshold": "BLOCK_NONE",
155
+ },
156
+ ]
157
+
158
+ model = genai.GenerativeModel(model_name='gemini-pro')
159
  response = model.generate_content([prompt, chunk],
160
+ #generation_config=generation_config,
161
+ safety_settings=safety_settings
 
 
 
 
 
162
  )
163
  translated_chunks.append(response.text)
164
 
165
  # Join the translated chunks back together into a single string
166
+ response = '\n\n*Next Paragraph*\n\n'.join(translated_chunks)
167
 
168
  return response
169
 
 
245
  # ---------------------- 复制到剪贴板 ----------------------
246
  cp_btn.click(fn=cp_text, inputs=[outputs_tr_text], outputs=[])
247
  cp_clear_btn.click(fn=cp_clear, inputs=[], outputs=[])
248
+
249
 
250
  ocr_tr.launch(inbrowser=True)
251