arj7192 commited on
Commit
83d31ee
·
verified ·
1 Parent(s): e115b98

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -5
app.py CHANGED
@@ -1,5 +1,6 @@
1
  import spaces
2
  import os
 
3
  import gradio as gr
4
  import easyocr
5
  import numpy as np
@@ -21,6 +22,54 @@ from pipeline_flux_controlnet_inpaint import FluxControlNetInpaintingPipeline
21
  import huggingface_hub
22
  huggingface_hub.login(os.getenv('HF_TOKEN_FLUX'))
23
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24
  bubble_detection_model = YOLO("speech_bubble_model.pt")
25
 
26
  language_to_ocr = {
@@ -102,12 +151,9 @@ def localize_boxes(merged_results, img_boxes, source_language, target_language):
102
  print(merged_results)
103
 
104
  prompt = f"""You are an expert translator and localization specialist with deep understanding of both {source_language} and {target_language} cultures.
105
-
106
  Task: Translate the detected text while preserving the cultural context and maintaining visual harmony. Make the results in capital letters.
107
-
108
  Source Text and Coordinates:
109
  {merged_results}
110
-
111
  Requirements:
112
  1. Maintain the original meaning and tone while adapting to {target_language} cultural context
113
  2. Keep translations concise and visually balanced (similar character length when possible)
@@ -119,10 +165,8 @@ def localize_boxes(merged_results, img_boxes, source_language, target_language):
119
  4. Consider the visual context from the provided image
120
  5. Use appropriate formality level for {target_language}
121
  6. Maintain any special formatting (if present)
122
-
123
  Format your response EXACTLY as a JSON-like list of dictionaries. Keep the box coordinates EXACTLY as they are, do not change them, only translate the text.
124
  [{{'box': [[x0, y0], [x1, y0], [x1, y1], [x0, y1]], 'text': 'translated_text'}}]
125
-
126
  Important: Only output the JSON format above, no explanations or additional text."""
127
 
128
  client = OpenAI(api_key=OPENAI_API_KEY)
 
1
  import spaces
2
  import os
3
+ import requests
4
  import gradio as gr
5
  import easyocr
6
  import numpy as np
 
22
  import huggingface_hub
23
  huggingface_hub.login(os.getenv('HF_TOKEN_FLUX'))
24
 
25
+
26
+ def download_from_github(repo, filepath, destination, token):
27
+ """
28
+ Download a file from a private GitHub repository.
29
+
30
+ Args:
31
+ repo (str): The GitHub repository in the format "owner/repo".
32
+ filepath (str): The path to the file in the repository.
33
+ destination (str): The local file path to save the downloaded file.
34
+ token (str): The GitHub Personal Access Token.
35
+
36
+ Returns:
37
+ None
38
+ """
39
+ url = f"https://api.github.com/repos/{repo}/contents/{filepath}"
40
+ headers = {"Authorization": f"token {token}"}
41
+ response = requests.get(url, headers=headers)
42
+
43
+ if response.status_code == 200:
44
+ file_content = response.json().get('content')
45
+ if file_content:
46
+ os.makedirs(os.path.dirname(destination), exist_ok=True)
47
+ with open(destination, 'wb') as f:
48
+ f.write(base64.b64decode(file_content))
49
+ print(f"Downloaded {filepath} to {destination}")
50
+ else:
51
+ raise Exception(f"File content not found for {filepath} in {repo}")
52
+ else:
53
+ raise Exception(f"Failed to download {filepath} from {repo}. Status code: {response.status_code}")
54
+
55
+ # Example: Define GitHub repo and file paths
56
+ repo_name = "arj7192/NativDemoLocal"
57
+ github_files = {
58
+ "speech_bubble_model.pt": "speech_bubble_model.pt",
59
+ "craft_mlt_25k.pth": "craft_mlt_25k.pth",
60
+ "english_g2.pth": "english_g2.pth",
61
+ "korean_g2.pth": "korean_g2.pth",
62
+ "latin_g2.pth": "latin_g2.pth",
63
+ "zh_sim_g2.pth": "zh_sim_g2.pth",
64
+ }
65
+
66
+ token = os.getenv("HF_GITHUB_TOKEN")
67
+
68
+ # Download each file
69
+ for filename, repo_path in github_files.items():
70
+ destination_path = f"{filename}"
71
+ download_from_github(repo_name, repo_path, destination_path, token)
72
+
73
  bubble_detection_model = YOLO("speech_bubble_model.pt")
74
 
75
  language_to_ocr = {
 
151
  print(merged_results)
152
 
153
  prompt = f"""You are an expert translator and localization specialist with deep understanding of both {source_language} and {target_language} cultures.
 
154
  Task: Translate the detected text while preserving the cultural context and maintaining visual harmony. Make the results in capital letters.
 
155
  Source Text and Coordinates:
156
  {merged_results}
 
157
  Requirements:
158
  1. Maintain the original meaning and tone while adapting to {target_language} cultural context
159
  2. Keep translations concise and visually balanced (similar character length when possible)
 
165
  4. Consider the visual context from the provided image
166
  5. Use appropriate formality level for {target_language}
167
  6. Maintain any special formatting (if present)
 
168
  Format your response EXACTLY as a JSON-like list of dictionaries. Keep the box coordinates EXACTLY as they are, do not change them, only translate the text.
169
  [{{'box': [[x0, y0], [x1, y0], [x1, y1], [x0, y1]], 'text': 'translated_text'}}]
 
170
  Important: Only output the JSON format above, no explanations or additional text."""
171
 
172
  client = OpenAI(api_key=OPENAI_API_KEY)