Hyeonseo commited on
Commit
9a63602
β€’
1 Parent(s): 5f3cbcc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -6
app.py CHANGED
@@ -1,21 +1,62 @@
1
  import gradio as gr
 
 
 
2
 
 
 
 
 
3
 
4
- def translate(text):
5
- return "translate_function"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
 
7
  inputs = [
8
- gr.inputs.Textbox(lines=10, label="Input Open API Key"),
9
  gr.inputs.File(label="Upload MDX File")
10
  ]
11
 
12
  outputs = gr.outputs.Textbox(label="Translation")
13
 
14
  def translate_with_upload(text, file):
 
 
 
15
  if file is not None:
16
- # 파일 μ—…λ‘œλ“œ μ‹œ 파일 λ‚΄μš©μ„ μ½μ–΄μ˜΅λ‹ˆλ‹€.
17
- text = file.read().decode('utf-8')
18
- return translate(text)
 
 
 
 
 
 
 
 
 
 
 
19
 
20
  prompt_translate = gr.Interface(
21
  fn=translate_with_upload,
 
1
  import gradio as gr
2
+ import subprocess
3
+ import openapi
4
+ import time
5
 
6
+ def translate(text_input, openapi_key):
7
+ openai.api_key = openapi_key
8
+
9
+ text_list = text_input.split('\n')[12:]
10
 
11
+ reply = []
12
+
13
+ for i in range(0,len(text_list)+9,10):
14
+ content = """What do these sentences about Hugging Face Transformers (a machine learning library) mean in Korean? Please do not translate the word after a πŸ€— emoji as it is a product name. Please ignore the video and image and translate only the sentences I provided. Ignore the contents of the iframe tag.
15
+ ```md
16
+ %s"""%'\n'.join(text_list[i:i+10])
17
+
18
+ chat = openai.ChatCompletion.create(
19
+ model = "gpt-3.5-turbo-0301", messages=[
20
+ {"role": "system",
21
+ "content": content},])
22
+
23
+ print("질문")
24
+ print(content)
25
+ print("응닡")
26
+ print(chat.choices[0].message.content)
27
+
28
+ reply.append(chat.choices[0].message.content)
29
+
30
+ time.sleep(20)
31
+
32
+ return reply
33
 
34
  inputs = [
35
+ gr.inputs.Textbox(lines=2, label="Input Open API Key"),
36
  gr.inputs.File(label="Upload MDX File")
37
  ]
38
 
39
  outputs = gr.outputs.Textbox(label="Translation")
40
 
41
  def translate_with_upload(text, file):
42
+
43
+ openapi_key = text
44
+
45
  if file is not None:
46
+ # 파일 이름을 μΆ”μΆœν•©λ‹ˆλ‹€.
47
+ filename = os.path.basename(file.name)
48
+ # μƒˆλ‘œμš΄ 파일 이름을 μƒμ„±ν•©λ‹ˆλ‹€.
49
+ new_filename = f"text_{filename}"
50
+ # sed λͺ…λ Ήμ–΄λ₯Ό μ‚¬μš©ν•˜μ—¬ νŒŒμΌμ„ λ³€ν™˜ν•©λ‹ˆλ‹€.
51
+ subprocess.run(f"sed '/```/,/```/d' {filename} | sed '/^|.*|$/d' | sed '/^$/N;/^\n$/D' > {new_filename}", shell=True)
52
+ # μƒˆλ‘œμš΄ νŒŒμΌμ„ μ—΄μ–΄μ„œ λ‚΄μš©μ„ μ½μ–΄μ˜΅λ‹ˆλ‹€.
53
+ with open(new_filename, 'r') as f:
54
+ text_input = f.read()
55
+ else:
56
+ # 파일이 μ—…λ‘œλ“œλ˜μ§€ μ•Šμ€ 경우, 빈 λ¬Έμžμ—΄λ‘œ μ΄ˆκΈ°ν™”ν•©λ‹ˆλ‹€.
57
+ text_input = ""
58
+
59
+ return translate(text_input, openapi_key)
60
 
61
  prompt_translate = gr.Interface(
62
  fn=translate_with_upload,