wanda222 commited on
Commit
4ef4a96
ยท
verified ยท
1 Parent(s): 66af148

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +73 -0
app.py ADDED
@@ -0,0 +1,73 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import requests
3
+ import os
4
+ from openai import OpenAI
5
+
6
+ UPSTAGE_API_KEY = os.getenv("UPSTAGE_API_KEY")
7
+
8
+
9
+ def extract_text_from_image(filename):
10
+ # Define the API endpoint
11
+ url = "https://api.upstage.ai/v1/document-digitization"
12
+
13
+ # Set the authorization header with your API key
14
+ headers = {'Authorization': f'Bearer {UPSTAGE_API_KEY}'}
15
+
16
+ # Open the PDF file in binary mode and attach it to the request
17
+ filename = 'letter.jpeg'
18
+ files = {"document": open(filename, "rb")}
19
+ data = {"model": "ocr"}
20
+
21
+ response = requests.post(url, headers=headers, files=files, data=data)
22
+
23
+ if response.status_code == 200:
24
+ text = response.json().get("text", "")
25
+ return text.strip()
26
+ else:
27
+ return f"OCR ์‹คํŒจ: {response.status_code} - {response.text}"
28
+
29
+ def translate_text_with_solar(english_text):
30
+ # Initialize the OpenAI client for Solar LLM
31
+ client = OpenAI(
32
+ api_key=UPSTAGE_API_KEY,
33
+ base_url="https://api.upstage.ai/v1"
34
+ )
35
+ prompt = "์•„๋ž˜๋Š” ์˜์–ด ํŽธ์ง€๋ฅผ OCR์„ ํ†ตํ•ด ์ถ”์ถœํ•œ ๋‚ด์šฉ์ž…๋‹ˆ๋‹ค. ์˜์–ด๋ฅผ ํ•œ๊ตญ์–ด๋กœ ๋ฒˆ์—ญํ•˜๊ณ , ํ•œ๊ตญ์–ด ์–ด์ˆœ, ์–ด๋ฒ•์— ๋งž๋„๋ก ์นœ๊ทผํ•œ ์–ดํˆฌ๋กœ ์œค๋ฌธํ•œ ์ตœ์ข… ๊ฒฐ๊ณผ๋งŒ ํ•œ๊ธ€๋กœ ์ž‘์„ฑํ•ด์ค˜. "
36
+ response = client.chat.completions.create(
37
+ model="solar-pro",
38
+ messages=[{"role": "user", "content": prompt}],
39
+ temperature=0.5,
40
+ max_tokens=1024
41
+ )
42
+ return response.choices[0].message.content
43
+
44
+ with gr.Blocks(title="๐Ÿ’Œ ์†๊ธ€์”จ ํŽธ์ง€ ๋ฒˆ์—ญ๊ธฐ") as demo:
45
+ gr.Markdown("##๐Ÿ’Œ ์†๊ธ€์”จ ํŽธ์ง€ ๋ฒˆ์—ญ๊ธฐ")
46
+ gr.Markdown("๐Ÿ“ท ํŽธ์ง€ ์ด๋ฏธ์ง€๋ฅผ ์—…๋กœ๋“œํ•˜๋ฉด Upstage Docuemnt OCR์ด ์˜์–ด ํ…์ŠคํŠธ๋ฅผ ์ถ”์ถœํ•˜๊ณ ,\n๐ŸŒ ๋ฒˆ์—ญํ•˜๊ธฐ ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด Solar LLM์„ ํ˜ธ์ถœํ•˜์—ฌ ํ•œ๊ตญ์–ด๋กœ ๋ฒˆ์—ญํ•ฉ๋‹ˆ๋‹ค!")
47
+
48
+ with gr.Row():
49
+ # ์™ผ์ชฝ: ์ด๋ฏธ์ง€ ์—…๋กœ๋“œ
50
+ with gr.Column(scale=1):
51
+ image_input = gr.Image(type="pil", label="๐Ÿ–ผ๏ธ ํŽธ์ง€ ์ด๋ฏธ์ง€ ์—…๋กœ๋“œ")
52
+
53
+ # ์˜ค๋ฅธ์ชฝ: ํ…์ŠคํŠธ ๊ฒฐ๊ณผ
54
+ with gr.Column(scale=2):
55
+ english_box = gr.Textbox(label="๐Ÿ“ ์ถ”์ถœ๋œ ์˜์–ด ํ…์ŠคํŠธ", lines=10)
56
+ translate_button = gr.Button("๐ŸŒ ๋ฒˆ์—ญํ•˜๊ธฐ")
57
+ korean_box = gr.Textbox(label="๐Ÿ‡ฐ๐Ÿ‡ท ๋ฒˆ์—ญ๋œ ํ•œ๊ตญ์–ด ํ…์ŠคํŠธ", lines=10)
58
+ # with gr.Row():
59
+ # image_input = gr.Image(type="pil", label="1๏ธโƒฃ ์˜์–ด ํŽธ์ง€ ์ด๋ฏธ์ง€ ์—…๋กœ๋“œ")
60
+ # english_box = gr.Textbox(label="๐Ÿ“ ์ถ”์ถœ๋œ ์˜์–ด ํ…์ŠคํŠธ", lines=10)
61
+
62
+ # with gr.Row():
63
+ # translate_button = gr.Button("๐ŸŒ ๋ฒˆ์—ญํ•˜๊ธฐ")
64
+ # korean_box = gr.Textbox(label="๐Ÿ‡ฐ๐Ÿ‡ท ๋ฒˆ์—ญ๋œ ํ•œ๊ตญ์–ด ํ…์ŠคํŠธ", lines=10)
65
+
66
+ # Step 1: ์ด๋ฏธ์ง€ ์—…๋กœ๋“œ ์‹œ OCR ์‹คํ–‰
67
+ image_input.change(fn=extract_text_from_image, inputs=image_input, outputs=english_box)
68
+
69
+ # Step 2: ๋ฒ„ํŠผ ๋ˆ„๋ฅด๋ฉด ๋ฒˆ์—ญ
70
+ translate_button.click(fn=translate_text_with_solar, inputs=english_box, outputs=korean_box)
71
+
72
+ if __name__ == "__main__":
73
+ demo.launch()