Spaces:
Runtime error
Runtime error
Commit
·
07420e6
1
Parent(s):
8aef166
add utils
Browse files- utils/__init__.py +2 -0
- utils/example.py +264 -0
- utils/utils.py +77 -0
utils/__init__.py
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from .utils import *
|
| 2 |
+
from .example import *
|
utils/example.py
ADDED
|
@@ -0,0 +1,264 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
SIDEBAR_INFO = """
|
| 2 |
+
<div class="contributors font-body text-bold">
|
| 3 |
+
<a class="contributor comma"> NamLH31,</a>
|
| 4 |
+
<a class="contributor comma"> MinhNA4,</a>
|
| 5 |
+
<a class="contributor comma"> DungNM31,</a>
|
| 6 |
+
<a class="contributor comma"> AnhDTV7,</a>
|
| 7 |
+
<a class="contributor comma"> AnhTVK</a>
|
| 8 |
+
</div>
|
| 9 |
+
"""
|
| 10 |
+
|
| 11 |
+
LEADER_INFOR = """
|
| 12 |
+
Dr. Bùi Duy Quốc Nghị (NghiBDQ)\n
|
| 13 |
+
Chief Scientist of AI4Code team at FPT Software AI Center
|
| 14 |
+
"""
|
| 15 |
+
|
| 16 |
+
NAM_INFOR = """
|
| 17 |
+
Lê Hải Nam (NamLH31)\n
|
| 18 |
+
AI Resident at FPT Software AI Center
|
| 19 |
+
"""
|
| 20 |
+
|
| 21 |
+
DUNG_INFOR = """
|
| 22 |
+
Nguyễn Mạnh Dũng (DungNM31)\n
|
| 23 |
+
AI Resident at FPT Software AI Center
|
| 24 |
+
"""
|
| 25 |
+
|
| 26 |
+
MINH_INFOR = """
|
| 27 |
+
Nguyễn Anh Mimh (MinhNA4)\n
|
| 28 |
+
AI Engineer at FPT Software AI Center
|
| 29 |
+
"""
|
| 30 |
+
|
| 31 |
+
VA_INFOR = """
|
| 32 |
+
Đậu Thị Vân Anh (AnhDTV7)\n
|
| 33 |
+
AI Resident at FPT Software AI Center
|
| 34 |
+
"""
|
| 35 |
+
|
| 36 |
+
KA_INFOR = """
|
| 37 |
+
Trần Vũ Kim Anh (AnhTVK)\n
|
| 38 |
+
Software Developer at FPT Software AI Center
|
| 39 |
+
"""
|
| 40 |
+
|
| 41 |
+
GROUP_INFO = """
|
| 42 |
+
<div class="story-box font-body">
|
| 43 |
+
<p>
|
| 44 |
+
Docify-Lab is from FPT Software AI Center.\n
|
| 45 |
+
We focus on exploring new ways to address a lot of software engineering challenges.
|
| 46 |
+
</p>
|
| 47 |
+
"""
|
| 48 |
+
|
| 49 |
+
STORY = """
|
| 50 |
+
<div class="story-box font-body">
|
| 51 |
+
<p>
|
| 52 |
+
Docify-Lab is designed to assist developers with many different coding tasks.\n
|
| 53 |
+
By leveraging the latest AI technology, our product help developers save time and effort, allowing them to focus on creating the best possible code. \n
|
| 54 |
+
Now, developers can work more efficiently and effectively than ever before.
|
| 55 |
+
</p>
|
| 56 |
+
"""
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
TRANS_EXAMPLE1 = """
|
| 60 |
+
def partition(array, low, high):
|
| 61 |
+
pivot = array[high]
|
| 62 |
+
i = low - 1
|
| 63 |
+
|
| 64 |
+
for j in range(low, high):
|
| 65 |
+
if array[j] <= pivot:
|
| 66 |
+
i = i + 1
|
| 67 |
+
|
| 68 |
+
tmp = array[i]
|
| 69 |
+
array[i] = array[j]
|
| 70 |
+
array[j] = tmp
|
| 71 |
+
|
| 72 |
+
tmp = array[i + 1]
|
| 73 |
+
array[i + 1] = array[high]
|
| 74 |
+
array[high] = tmp
|
| 75 |
+
return i + 1
|
| 76 |
+
|
| 77 |
+
|
| 78 |
+
def quickSort(array, low, high):
|
| 79 |
+
if low < high:
|
| 80 |
+
pi = partition(array, low, high)
|
| 81 |
+
quickSort(array, low, pi - 1)
|
| 82 |
+
quickSort(array, pi + 1, high)
|
| 83 |
+
"""
|
| 84 |
+
|
| 85 |
+
TRANS_EXAMPLE2 = """public String getMaxValue(List<Object> list, int first, int last) {
|
| 86 |
+
List<Float> floatList = new ArrayList<>();
|
| 87 |
+
for (int i = 0; i < list.size(); i++) {
|
| 88 |
+
Float prova2 = ((Double) list.get(i)).floatValue();
|
| 89 |
+
floatList.add(prova2);
|
| 90 |
+
}
|
| 91 |
+
float max = Float.MIN_VALUE;
|
| 92 |
+
String maxValue = "";
|
| 93 |
+
for (int i = first; i < last; i++) {
|
| 94 |
+
if (floatList.get(i) > max) {
|
| 95 |
+
max = floatList.get(i);
|
| 96 |
+
}
|
| 97 |
+
}
|
| 98 |
+
maxValue = String.format("%.1f", max);
|
| 99 |
+
return maxValue;
|
| 100 |
+
}
|
| 101 |
+
"""
|
| 102 |
+
|
| 103 |
+
TRANS_EXAMPLE3 = """public class Calculator
|
| 104 |
+
{
|
| 105 |
+
public int dot(int x, int y){
|
| 106 |
+
return x * y;
|
| 107 |
+
}
|
| 108 |
+
|
| 109 |
+
public int add(int x, int y){
|
| 110 |
+
add_rs = x + y;
|
| 111 |
+
return rs;
|
| 112 |
+
}
|
| 113 |
+
|
| 114 |
+
public int divide(float a, float b){
|
| 115 |
+
result = a/b;
|
| 116 |
+
return result;
|
| 117 |
+
}
|
| 118 |
+
|
| 119 |
+
public String nothing(){
|
| 120 |
+
return "nothing";
|
| 121 |
+
}
|
| 122 |
+
}
|
| 123 |
+
"""
|
| 124 |
+
|
| 125 |
+
|
| 126 |
+
COM_EXAMPLE1 = """def sum2num (a, b):"""
|
| 127 |
+
|
| 128 |
+
|
| 129 |
+
COM_EXAMPLE2 = """
|
| 130 |
+
def is_prime(n):
|
| 131 |
+
for i in range(2,int(n/2)):
|
| 132 |
+
if (n%i) =
|
| 133 |
+
"""
|
| 134 |
+
|
| 135 |
+
COM_EXAMPLE3 = """
|
| 136 |
+
public static int[] stringToIntegerArray(String input) {
|
| 137 |
+
input = input.trim();
|
| 138 |
+
input = input.substring(1, input.length() - 1);
|
| 139 |
+
if (input.length() == 0) {
|
| 140 |
+
return new int[0];
|
| 141 |
+
}
|
| 142 |
+
|
| 143 |
+
String[] parts = input.split(",");
|
| 144 |
+
int[] output = new int[parts.
|
| 145 |
+
"""
|
| 146 |
+
|
| 147 |
+
|
| 148 |
+
RE_EXAMPLE1 = """
|
| 149 |
+
private JComboBox<String> initCombo(String[] items, int x, int y, String toolTip, Consumer consumerEvent) {
|
| 150 |
+
JComboBox<int> combo = new JComboBox<>(items);
|
| 151 |
+
combo.setBounds(x, y 140, 25);
|
| 152 |
+
combo.setToolTipText(toolTip);
|
| 153 |
+
combo.setCursor(new Cursor(Cursor.HAND_CURSOR));
|
| 154 |
+
combo.addItemListener(consumerEvent::accept);
|
| 155 |
+
window.add(combo);
|
| 156 |
+
return combo;
|
| 157 |
+
}
|
| 158 |
+
"""
|
| 159 |
+
|
| 160 |
+
RE_EXAMPLE2 = """
|
| 161 |
+
def bubbleSort(arr):
|
| 162 |
+
n = len(arr))
|
| 163 |
+
swapped = False
|
| 164 |
+
for i in range(j-1):
|
| 165 |
+
for j in range(0, n-i-1):
|
| 166 |
+
if arr[j] > arr[j + 1]:
|
| 167 |
+
swapped = True
|
| 168 |
+
arr[j], arr[j + 1] = arr[j + 1], arr[j]
|
| 169 |
+
if not swapped:
|
| 170 |
+
return
|
| 171 |
+
"""
|
| 172 |
+
|
| 173 |
+
RE_EXAMPLE3 = """
|
| 174 |
+
public int add(char x, int y){
|
| 175 |
+
add_rs = x + y;
|
| 176 |
+
return rs;
|
| 177 |
+
}
|
| 178 |
+
"""
|
| 179 |
+
|
| 180 |
+
CGEN_EXAMPLE1 = "check if a string contains character @"
|
| 181 |
+
CGEN_EXAMPLE2 = "Read csv file using pandas"
|
| 182 |
+
CGEN_EXAMPLE3 = "Compare two numbers and return the bigger"
|
| 183 |
+
|
| 184 |
+
|
| 185 |
+
SUM_EXAMPLE1 = """
|
| 186 |
+
def binary_search(arr, low, high, x):
|
| 187 |
+
if high >= low:
|
| 188 |
+
|
| 189 |
+
mid = (high + low) // 2
|
| 190 |
+
|
| 191 |
+
if arr[mid] == x:
|
| 192 |
+
return mid
|
| 193 |
+
|
| 194 |
+
elif arr[mid] > x:
|
| 195 |
+
return binary_search(arr, low, mid - 1, x)
|
| 196 |
+
|
| 197 |
+
else:
|
| 198 |
+
return binary_search(arr, mid + 1, high, x)
|
| 199 |
+
|
| 200 |
+
else:
|
| 201 |
+
return -1
|
| 202 |
+
"""
|
| 203 |
+
SUM_EXAMPLE2 = """
|
| 204 |
+
def read_csv(filename):
|
| 205 |
+
df = pd.read_csv(filename)
|
| 206 |
+
return df
|
| 207 |
+
"""
|
| 208 |
+
SUM_EXAMPLE3 = """public class Calculator
|
| 209 |
+
{
|
| 210 |
+
public int dot(int x, int y){
|
| 211 |
+
return x * y;
|
| 212 |
+
}
|
| 213 |
+
|
| 214 |
+
public int add(int x, int y){
|
| 215 |
+
add_rs = x + y;
|
| 216 |
+
return rs;
|
| 217 |
+
}
|
| 218 |
+
|
| 219 |
+
public int divide(float a, float b){
|
| 220 |
+
result = a/b;
|
| 221 |
+
return result;
|
| 222 |
+
}
|
| 223 |
+
|
| 224 |
+
public String nothing(){
|
| 225 |
+
return "nothing";
|
| 226 |
+
}
|
| 227 |
+
}
|
| 228 |
+
"""
|
| 229 |
+
|
| 230 |
+
TEST_EXAMPLE1 = """public class Calculator
|
| 231 |
+
{
|
| 232 |
+
public int add(int x, int y){
|
| 233 |
+
add_rs = x + y;
|
| 234 |
+
return rs;
|
| 235 |
+
}
|
| 236 |
+
}
|
| 237 |
+
"""
|
| 238 |
+
TEST_EXAMPLE2 = """public class Comparator
|
| 239 |
+
{
|
| 240 |
+
public int bigger_number(int x, int y){
|
| 241 |
+
if (x > y)
|
| 242 |
+
return x;
|
| 243 |
+
else
|
| 244 |
+
return y;
|
| 245 |
+
}
|
| 246 |
+
|
| 247 |
+
public int smaller_number(int x, int y){
|
| 248 |
+
if (x < y)
|
| 249 |
+
return x;
|
| 250 |
+
else
|
| 251 |
+
return y;
|
| 252 |
+
}
|
| 253 |
+
}
|
| 254 |
+
"""
|
| 255 |
+
TEST_EXAMPLE3 = """class TransformText {
|
| 256 |
+
static String upper(String str) {
|
| 257 |
+
str.toUpperCase()
|
| 258 |
+
}
|
| 259 |
+
|
| 260 |
+
static String lower(String str) {
|
| 261 |
+
str.toLowerCase()
|
| 262 |
+
}
|
| 263 |
+
}
|
| 264 |
+
"""
|
utils/utils.py
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import json
|
| 3 |
+
from typing import Dict, Any
|
| 4 |
+
import requests
|
| 5 |
+
|
| 6 |
+
from PIL import Image
|
| 7 |
+
import base64
|
| 8 |
+
from pathlib import Path
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
# from guesslang import Guess
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
# guessor = Guess()
|
| 15 |
+
|
| 16 |
+
def img_to_bytes(img_path):
|
| 17 |
+
img_bytes = Path(img_path).read_bytes()
|
| 18 |
+
encoded = base64.b64encode(img_bytes).decode()
|
| 19 |
+
return encoded
|
| 20 |
+
def img_to_html(img_path):
|
| 21 |
+
img_html = "<img src='data:image/png;base64,{}' class='img-fluid'>".format(
|
| 22 |
+
img_to_bytes(img_path)
|
| 23 |
+
)
|
| 24 |
+
return img_html
|
| 25 |
+
|
| 26 |
+
def request_api(url: str, data: Dict[str, Any]):
|
| 27 |
+
# _input: str,
|
| 28 |
+
# task: str,
|
| 29 |
+
# source_lgs: str=None,
|
| 30 |
+
# target_lgs: str=None
|
| 31 |
+
|
| 32 |
+
"""
|
| 33 |
+
Post request to API
|
| 34 |
+
|
| 35 |
+
Args:
|
| 36 |
+
_input (str): input data
|
| 37 |
+
task (str): task
|
| 38 |
+
url (str): api
|
| 39 |
+
source_lgs (str): Source languages
|
| 40 |
+
target_lgs (str): Target languages
|
| 41 |
+
|
| 42 |
+
Return:
|
| 43 |
+
respone (json)
|
| 44 |
+
"""
|
| 45 |
+
# data = {
|
| 46 |
+
# "input": _input,
|
| 47 |
+
# "task": task,
|
| 48 |
+
# "source_lgs": source_lgs,
|
| 49 |
+
# "target_lgs": target_lgs,
|
| 50 |
+
# }
|
| 51 |
+
|
| 52 |
+
headers = {
|
| 53 |
+
'Content-type': 'application/json',
|
| 54 |
+
'Accept': 'text/plain'
|
| 55 |
+
}
|
| 56 |
+
response = requests.post(url, data=json.dumps(data), headers=headers)
|
| 57 |
+
return response
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
def load_image_from_local(image_path, image_resize=None):
|
| 61 |
+
image = Image.open(image_path)
|
| 62 |
+
|
| 63 |
+
if isinstance(image_resize, tuple):
|
| 64 |
+
image = image.resize(image_resize)
|
| 65 |
+
return image
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
def detect_lang(url, input_):
|
| 69 |
+
# name = guessor.language_name(input_)
|
| 70 |
+
responde = request_api(url, {
|
| 71 |
+
'input': input_,
|
| 72 |
+
'task': 'language_detection',
|
| 73 |
+
'source_lgs': None,
|
| 74 |
+
'target_lgs': None
|
| 75 |
+
})
|
| 76 |
+
name = json.loads(responde.text)['output']
|
| 77 |
+
return name
|