Spaces:
Running
Running
Navid Arabi
commited on
Commit
·
ac40033
1
Parent(s):
6eebaaf
test mysql
Browse files- .gitignore +2 -0
- app.py +38 -0
- config.py +9 -0
- database.py +152 -0
- db_test.py +32 -0
- models.py +81 -0
- requirements.txt +3 -0
.gitignore
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
venv/
|
2 |
+
.env
|
app.py
ADDED
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import mysql.connector
|
2 |
+
from mysql.connector import Error
|
3 |
+
|
4 |
+
|
5 |
+
config = {
|
6 |
+
"host": "annotation-db.apps.teh2.abrhapaas.com",
|
7 |
+
"user": "navid",
|
8 |
+
"password": "ZUJSK!1V!PF4ZEnIaylX",
|
9 |
+
"charset": "utf8mb4",
|
10 |
+
"port": 32107,
|
11 |
+
"use_unicode": True
|
12 |
+
}
|
13 |
+
|
14 |
+
|
15 |
+
import gradio as gr
|
16 |
+
|
17 |
+
def greet(name):
|
18 |
+
try:
|
19 |
+
conn = mysql.connector.connect(**config)
|
20 |
+
if conn.is_connected():
|
21 |
+
cursor = conn.cursor()
|
22 |
+
cursor.execute("SELECT VERSION()")
|
23 |
+
version = cursor.fetchone()
|
24 |
+
return f"🟢 MySQL Server Version: {version[0]}"
|
25 |
+
|
26 |
+
except Error as e:
|
27 |
+
return f"❌ error: {e}"
|
28 |
+
|
29 |
+
finally:
|
30 |
+
if 'cursor' in locals():
|
31 |
+
cursor.close()
|
32 |
+
if 'conn' in locals() and conn.is_connected():
|
33 |
+
conn.close()
|
34 |
+
print("⛔️Closed.")
|
35 |
+
|
36 |
+
|
37 |
+
demo = gr.Interface(fn=greet, inputs="text", outputs="text")
|
38 |
+
demo.launch()
|
config.py
ADDED
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
|
3 |
+
|
4 |
+
db_config = {
|
5 |
+
'user': os.environ.get('DB_USER'),
|
6 |
+
'password': os.environ.get('DB_PASSWORD'),
|
7 |
+
'host': os.environ.get('DB_HOST'),
|
8 |
+
'database': os.environ.get('DB_NAME')
|
9 |
+
}
|
database.py
ADDED
@@ -0,0 +1,152 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from sqlalchemy import create_engine, Column, Integer, String, Float, Boolean, DateTime, ForeignKey
|
2 |
+
from sqlalchemy.orm import sessionmaker, relationship, declarative_base
|
3 |
+
from pydantic import BaseModel, ConfigDict
|
4 |
+
from typing import List
|
5 |
+
from datetime import datetime
|
6 |
+
|
7 |
+
# ایجاد مدلهای SQLAlchemy
|
8 |
+
Base = declarative_base()
|
9 |
+
|
10 |
+
class Annotator(Base):
|
11 |
+
__tablename__ = 'annotators'
|
12 |
+
|
13 |
+
id = Column(Integer, primary_key=True)
|
14 |
+
name = Column(String, nullable=False, unique=True)
|
15 |
+
password = Column(String, nullable=False)
|
16 |
+
last_login = Column(DateTime)
|
17 |
+
is_active = Column(Boolean, default=True)
|
18 |
+
annotations = relationship('Annotation', backref='annotator')
|
19 |
+
|
20 |
+
class AudioTrim(Base):
|
21 |
+
__tablename__ = 'audio_trims'
|
22 |
+
|
23 |
+
id = Column(Integer, primary_key=True)
|
24 |
+
start = Column(Float, nullable=False)
|
25 |
+
end = Column(Float, nullable=False)
|
26 |
+
annotation_id = Column(Integer, ForeignKey('annotations.id'))
|
27 |
+
|
28 |
+
class Annotation(Base):
|
29 |
+
__tablename__ = 'annotations'
|
30 |
+
|
31 |
+
id = Column(Integer, primary_key=True)
|
32 |
+
annotated_subtitle = Column(String, nullable=False)
|
33 |
+
is_first_phase_accepted = Column(Boolean, nullable=False)
|
34 |
+
create_at = Column(DateTime, nullable=False)
|
35 |
+
update_at = Column(DateTime, nullable=False)
|
36 |
+
audio_trims = relationship('AudioTrim', cascade='all, delete', backref='annotation')
|
37 |
+
json_data_id = Column(Integer, ForeignKey('json_data.id'))
|
38 |
+
annotator_id = Column(Integer, ForeignKey('annotators.id'))
|
39 |
+
|
40 |
+
class JsonData(Base):
|
41 |
+
__tablename__ = 'json_data'
|
42 |
+
|
43 |
+
id = Column(Integer, primary_key=True)
|
44 |
+
voice_name = Column(String, nullable=False)
|
45 |
+
original_subtitle = Column(String, nullable=False)
|
46 |
+
ignore_it = Column(Boolean, nullable=False)
|
47 |
+
is_approved_in_second_phase = Column(Boolean, nullable=False)
|
48 |
+
annotations = relationship('Annotation', cascade='all, delete', backref='json_data')
|
49 |
+
|
50 |
+
# ایجاد مدلهای Pydantic
|
51 |
+
class AudioTrimModel(BaseModel):
|
52 |
+
start: float
|
53 |
+
end: float
|
54 |
+
model_config = ConfigDict(from_attributes=True)
|
55 |
+
|
56 |
+
class AnnotationModel(BaseModel):
|
57 |
+
annotator: str
|
58 |
+
annotated_subtitle: str
|
59 |
+
is_first_phase_accepted: bool
|
60 |
+
create_at: datetime
|
61 |
+
update_at: datetime
|
62 |
+
audio_trims: List[AudioTrimModel]
|
63 |
+
model_config = ConfigDict(from_attributes=True)
|
64 |
+
|
65 |
+
class JsonDataModel(BaseModel):
|
66 |
+
id: int
|
67 |
+
voice_name: str
|
68 |
+
original_subtitle: str
|
69 |
+
ignore_it: bool
|
70 |
+
is_approved_in_second_phase: bool
|
71 |
+
annotations: List[AnnotationModel]
|
72 |
+
model_config = ConfigDict(from_attributes=True)
|
73 |
+
|
74 |
+
# ایجاد جلسه
|
75 |
+
DB_URL = "mysql+pymysql://user:password@localhost/testdb" # اطلاعات کاربری و دیتابیس را ویرایش کنید
|
76 |
+
engine = create_engine(DB_URL)
|
77 |
+
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
|
78 |
+
|
79 |
+
def initialize_database():
|
80 |
+
Base.metadata.create_all(bind=engine)
|
81 |
+
|
82 |
+
# تابع ذخیره دادهها در دیتابیس
|
83 |
+
def save_json_data(json_data_dict):
|
84 |
+
db = SessionLocal()
|
85 |
+
try:
|
86 |
+
# استفاده از Pydantic برای اعتبارسنجی دادهها
|
87 |
+
json_data = JsonDataModel(**json_data_dict)
|
88 |
+
|
89 |
+
for annotation in json_data.annotations:
|
90 |
+
# تامین موجودیت annotator
|
91 |
+
db_annotator = db.query(Annotator).filter_by(name=annotation.annotator).first()
|
92 |
+
if not db_annotator:
|
93 |
+
db_annotator = Annotator(name=annotation.annotator, password="default_password")
|
94 |
+
db.add(db_annotator)
|
95 |
+
db.commit() # اضافه کنید تا id اختصاص داده شود
|
96 |
+
|
97 |
+
# تبدیل مدل Pydantic به مدل SQLAlchemy
|
98 |
+
db_annotation = Annotation(
|
99 |
+
annotated_subtitle=annotation.annotated_subtitle,
|
100 |
+
is_first_phase_accepted=annotation.is_first_phase_accepted,
|
101 |
+
create_at=annotation.create_at,
|
102 |
+
update_at=annotation.update_at,
|
103 |
+
audio_trims=[
|
104 |
+
AudioTrim(start=trim.start, end=trim.end) for trim in annotation.audio_trims
|
105 |
+
],
|
106 |
+
annotator=db_annotator
|
107 |
+
)
|
108 |
+
|
109 |
+
db_json_data = JsonData(
|
110 |
+
id=json_data.id,
|
111 |
+
voice_name=json_data.voice_name,
|
112 |
+
original_subtitle=json_data.original_subtitle,
|
113 |
+
ignore_it=json_data.ignore_it,
|
114 |
+
is_approved_in_second_phase=json_data.is_approved_in_second_phase,
|
115 |
+
annotations=[db_annotation]
|
116 |
+
)
|
117 |
+
|
118 |
+
db.add(db_json_data)
|
119 |
+
db.commit()
|
120 |
+
|
121 |
+
except Exception as e:
|
122 |
+
db.rollback()
|
123 |
+
print(e)
|
124 |
+
finally:
|
125 |
+
db.close()
|
126 |
+
|
127 |
+
# فراخوانی تابع ایجاد جدولها و افزودن داده
|
128 |
+
initialize_database()
|
129 |
+
|
130 |
+
# دادهی نمونه برای افزودن
|
131 |
+
json_data = {
|
132 |
+
"id": 16562,
|
133 |
+
"voice_name": "voice1.wav",
|
134 |
+
"original_subtitle": "خلاصه با دلی ناراحت این کار رو کرد کاری رو که به نظرم میاد کار عقلانی بوده و رفتن رسیدن همون جایی که خاطره خداحافظی ۶ سال پیش رو واسه شکلتون زنده کرد اون بار به عنوان invalid اون بار به عنوان ادمی که از پس کار برنیومده",
|
135 |
+
"ignore_it": False,
|
136 |
+
"annotations": [
|
137 |
+
{
|
138 |
+
"annotator": "amin76",
|
139 |
+
"annotated_subtitle": "خلاصه با دلی ناراحت این کار رو کرد کاری رو که به نظرم میاد کار عقلانی بوده و رفتن رسیدن همون جایی که خاطره خداحافظی شش سال پیش رو واسه شکلتون زنده کرد اون بار به عنوان اينوليد اون بار به عنوان آدمی که از پس کار برنیومده",
|
140 |
+
"audio_trims": [
|
141 |
+
{"start": 14.5, "end": 15.0},
|
142 |
+
{"start": 14.3, "end": 15.0}
|
143 |
+
],
|
144 |
+
"is_first_phase_accepted": False,
|
145 |
+
"create_at": "2025-05-20T16:05:53.138811",
|
146 |
+
"update_at": "2025-05-20T16:06:28.525393"
|
147 |
+
}
|
148 |
+
],
|
149 |
+
"is_approved_in_second_phase": False
|
150 |
+
}
|
151 |
+
|
152 |
+
save_json_data(json_data)
|
db_test.py
ADDED
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import mysql.connector
|
2 |
+
from mysql.connector import Error
|
3 |
+
|
4 |
+
|
5 |
+
config = {
|
6 |
+
"host": "annotation-db.apps.teh2.abrhapaas.com",
|
7 |
+
"user": "navid",
|
8 |
+
"password": "ZUJSK!1V!PF4ZEnIaylX",
|
9 |
+
"charset": "utf8mb4",
|
10 |
+
"port": 32107,
|
11 |
+
"use_unicode": True
|
12 |
+
}
|
13 |
+
|
14 |
+
try:
|
15 |
+
conn = mysql.connector.connect(**config)
|
16 |
+
if conn.is_connected():
|
17 |
+
print("✅OK!")
|
18 |
+
|
19 |
+
cursor = conn.cursor()
|
20 |
+
cursor.execute("SELECT VERSION()")
|
21 |
+
version = cursor.fetchone()
|
22 |
+
print("🟢MySQL Server Version:", version[0])
|
23 |
+
|
24 |
+
except Error as e:
|
25 |
+
print("❌ error:", e)
|
26 |
+
|
27 |
+
finally:
|
28 |
+
if 'cursor' in locals():
|
29 |
+
cursor.close()
|
30 |
+
if 'conn' in locals() and conn.is_connected():
|
31 |
+
conn.close()
|
32 |
+
print("⛔️Closed.")
|
models.py
ADDED
@@ -0,0 +1,81 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from sqlalchemy import Column, Integer, String, Float, Boolean, DateTime, ForeignKey
|
2 |
+
from sqlalchemy.orm import relationship, declarative_base
|
3 |
+
from pydantic import BaseModel, ConfigDict
|
4 |
+
from typing import List
|
5 |
+
from datetime import datetime
|
6 |
+
|
7 |
+
Base = declarative_base()
|
8 |
+
|
9 |
+
class Annotator(Base):
|
10 |
+
__tablename__ = 'annotators'
|
11 |
+
|
12 |
+
id = Column(Integer, primary_key=True)
|
13 |
+
name = Column(String, nullable=False, unique=True)
|
14 |
+
password = Column(String, nullable=False)
|
15 |
+
last_login = Column(DateTime)
|
16 |
+
is_active = Column(Boolean, default=True)
|
17 |
+
annotations = relationship('Annotation', backref='annotator')
|
18 |
+
validators = relationship('Validator', backref='annotator')
|
19 |
+
annotation_intervals = relationship('AnnotationInterval', backref='annotator')
|
20 |
+
|
21 |
+
|
22 |
+
class Validator(Base):
|
23 |
+
__tablename__ = 'validators'
|
24 |
+
|
25 |
+
id = Column(Integer, primary_key=True)
|
26 |
+
annotator_id = Column(Integer, ForeignKey('annotators.id'))
|
27 |
+
validator_id = Column(Integer, ForeignKey('annotators.id'))
|
28 |
+
|
29 |
+
|
30 |
+
class AnnotationInterval(Base):
|
31 |
+
__tablename__ = 'annotation_intervals'
|
32 |
+
|
33 |
+
id = Column(Integer, primary_key=True)
|
34 |
+
annotator_id = Column(Integer, ForeignKey('annotators.id'))
|
35 |
+
start_index = Column(Integer, nullable=True)
|
36 |
+
end_index = Column(Integer, nullable=True)
|
37 |
+
|
38 |
+
|
39 |
+
class AudioTrim(Base):
|
40 |
+
__tablename__ = 'audio_trims'
|
41 |
+
|
42 |
+
id = Column(Integer, primary_key=True)
|
43 |
+
start = Column(Float, nullable=False)
|
44 |
+
end = Column(Float, nullable=False)
|
45 |
+
annotation_id = Column(Integer, ForeignKey('annotations.id'))
|
46 |
+
|
47 |
+
class Annotation(Base):
|
48 |
+
__tablename__ = 'annotations'
|
49 |
+
|
50 |
+
id = Column(Integer, primary_key=True)
|
51 |
+
annotated_subtitle = Column(String, nullable=False)
|
52 |
+
validated = Column(Boolean, nullable=False)
|
53 |
+
create_at = Column(DateTime, nullable=False)
|
54 |
+
update_at = Column(DateTime, nullable=False)
|
55 |
+
audio_trims = relationship('AudioTrim', cascade='all, delete', backref='annotation')
|
56 |
+
json_data_id = Column(Integer, ForeignKey('json_data.id'))
|
57 |
+
annotator_id = Column(Integer, ForeignKey('annotators.id'))
|
58 |
+
|
59 |
+
|
60 |
+
class AudioTrimModel(BaseModel):
|
61 |
+
start: float
|
62 |
+
end: float
|
63 |
+
model_config = ConfigDict(from_attributes=True)
|
64 |
+
|
65 |
+
class AnnotationModel(BaseModel):
|
66 |
+
annotator: str
|
67 |
+
annotated_subtitle: str
|
68 |
+
is_first_phase_accepted: bool
|
69 |
+
create_at: datetime
|
70 |
+
update_at: datetime
|
71 |
+
audio_trims: List[AudioTrimModel]
|
72 |
+
model_config = ConfigDict(from_attributes=True)
|
73 |
+
|
74 |
+
class JsonDataModel(BaseModel):
|
75 |
+
id: int
|
76 |
+
voice_name: str
|
77 |
+
original_subtitle: str
|
78 |
+
ignore_it: bool
|
79 |
+
is_approved_in_second_phase: bool
|
80 |
+
annotations: List[AnnotationModel]
|
81 |
+
model_config = ConfigDict(from_attributes=True)
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
gradio
|
2 |
+
sqlalchemy
|
3 |
+
pydantic
|