Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,124 +3,92 @@ import gradio as gr
|
|
| 3 |
import random
|
| 4 |
import os
|
| 5 |
import threading
|
|
|
|
| 6 |
from datasets import load_dataset
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
|
| 12 |
-
|
| 13 |
-
# 1. SQLAlchemy ์ค์ ๋ฐ ๋ชจ๋ธ ์ ์
|
| 14 |
-
#############################################
|
| 15 |
-
|
| 16 |
-
DATABASE_URL = "mysql://root:0112@127.0.0.1:3306/Taskmaster_sample_data"
|
| 17 |
-
|
| 18 |
-
# SQLite์ ๋์ ์ ๊ทผ์ ์ํด check_same_thread ์ต์
์ค์
|
| 19 |
-
engine = create_engine(DATABASE_URL, connect_args={"check_same_thread": False})
|
| 20 |
-
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
|
| 21 |
-
Base = declarative_base()
|
| 22 |
-
|
| 23 |
-
class Conversation(Base):
|
| 24 |
-
__tablename__ = "conversations"
|
| 25 |
-
id = Column(Integer, primary_key=True, index=True)
|
| 26 |
-
conversation_id = Column(String, index=True)
|
| 27 |
-
used = Column(Boolean, default=False)
|
| 28 |
-
overlapping = Column(String)
|
| 29 |
-
text = Column(Text)
|
| 30 |
-
human_message = Column(Text)
|
| 31 |
-
ai_message = Column(Text)
|
| 32 |
-
|
| 33 |
-
# ํ
์ด๋ธ ์์ฑ
|
| 34 |
-
Base.metadata.create_all(bind=engine)
|
| 35 |
-
|
| 36 |
-
# ๋์ ์ ๊ทผ์ ์ํ ์ ์ญ ๋ฝ
|
| 37 |
-
db_lock = threading.Lock()
|
| 38 |
-
|
| 39 |
-
#############################################
|
| 40 |
-
# 2. ๋ฐ์ดํฐ๋ฒ ์ด์ค ์ด๊ธฐํ ํจ์
|
| 41 |
-
#############################################
|
| 42 |
-
|
| 43 |
-
def init_db():
|
| 44 |
"""
|
| 45 |
-
|
|
|
|
| 46 |
"""
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
data =
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
)
|
| 64 |
-
session.add(conv)
|
| 65 |
-
session.commit()
|
| 66 |
-
finally:
|
| 67 |
-
session.close()
|
| 68 |
|
| 69 |
-
|
| 70 |
-
|
|
|
|
|
|
|
|
|
|
| 71 |
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
|
| 76 |
def get_random_row_from_dataset():
|
| 77 |
"""
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
validํ
|
| 82 |
-
- ํด๋น ๊ทธ๋ฃน์ ๋ชจ๋ ํ์ used ๊ฐ์ True๋ก
|
| 83 |
-
- ์ ํ๋ ๊ทธ๋ฃน ๋ด์์ overlapping ์ปฌ๋ผ์ด "TT"์ธ ์ฒซ ๋ฒ์งธ
|
| 84 |
"""
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
valid_groups
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
return None
|
| 106 |
-
finally:
|
| 107 |
-
session.close()
|
| 108 |
-
|
| 109 |
-
#############################################
|
| 110 |
-
# 4. ์ด๊ธฐ ๋ํ ์ค์ (์ ์ญ ๋ณ์ ์ค์ )
|
| 111 |
-
#############################################
|
| 112 |
|
| 113 |
-
|
| 114 |
-
|
|
|
|
|
|
|
| 115 |
human_message = "No valid conversation available."
|
| 116 |
ai_message = "No valid conversation available."
|
| 117 |
else:
|
| 118 |
-
raw_text =
|
| 119 |
-
human_message = raw_text.split("[turn]")[0].strip()
|
| 120 |
-
ai_message = raw_text.split("[turn]")[1].strip()
|
| 121 |
|
| 122 |
#############################################
|
| 123 |
-
#
|
| 124 |
#############################################
|
| 125 |
|
| 126 |
def get_initial_human_html():
|
|
@@ -149,44 +117,47 @@ def stream_human_message():
|
|
| 149 |
bubble_end = "</div>"
|
| 150 |
emoji_html = "<div class='emoji'>๐ง</div>"
|
| 151 |
wrapper_end = "</div>"
|
|
|
|
|
|
|
| 152 |
yield wrapper_start + bubble_start + bubble_end + emoji_html + wrapper_end
|
|
|
|
|
|
|
| 153 |
for i, ch in enumerate(human_message):
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
|
| 159 |
def submit_edit(edited_text):
|
| 160 |
"""
|
| 161 |
Submit ๋ฒํผ ํด๋ฆญ ์ ํธ์ถ๋๋ ํจ์.
|
| 162 |
-
1. ํธ์ง๋ human ๋ฉ์์ง(โ๏ธ ์๋ถ๋ถ)๋ฅผ ์ ํ์ผ๋ก
|
| 163 |
2. get_random_row_from_dataset()์ ํตํด ์๋ก์ด ๋ํ๋ฅผ ๊ฐ์ ธ์ค๊ณ , ์ ์ญ ๋ณ์ human_message์ ai_message๋ฅผ ์
๋ฐ์ดํธํฉ๋๋ค.
|
| 164 |
3. ์ด๊ธฐ ์ํ์ human ๋งํ์ ๊ณผ ai ๋งํ์ HTML์ ๋ฐํํ์ฌ ์ธํฐํ์ด์ค๋ฅผ ๋ฆฌ์
ํฉ๋๋ค.
|
| 165 |
"""
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
session.close()
|
| 190 |
new_human_html = get_initial_human_html()
|
| 191 |
new_ai_html = f"""
|
| 192 |
<div class="ai-wrapper" style="display: flex; align-items: flex-end; justify-content: flex-start; gap: 5px; width: 100%;">
|
|
@@ -197,11 +168,11 @@ def submit_edit(edited_text):
|
|
| 197 |
return new_human_html, new_ai_html
|
| 198 |
|
| 199 |
#############################################
|
| 200 |
-
#
|
| 201 |
#############################################
|
| 202 |
|
| 203 |
with gr.Blocks() as demo:
|
| 204 |
-
# (A) ํ์ด์ง ์๋จ ์คํฌ๋ฆฝํธ: Human ๋งํ์ ๋ด์ ๊ฐ <span data-index="..."
|
| 205 |
# ํด๋น ์์น์ โ๏ธ ์์ด์ฝ์ด ์ฝ์
๋๊ณ , ๊ทธ ์ดํ ํ
์คํธ๊ฐ ํ์์ผ๋ก ๋ณ๊ฒฝ๋ฉ๋๋ค.
|
| 206 |
gr.HTML(
|
| 207 |
"""
|
|
@@ -230,7 +201,8 @@ with gr.Blocks() as demo:
|
|
| 230 |
</script>
|
| 231 |
"""
|
| 232 |
)
|
| 233 |
-
|
|
|
|
| 234 |
gr.HTML(
|
| 235 |
"""
|
| 236 |
<script>
|
|
@@ -249,6 +221,7 @@ with gr.Blocks() as demo:
|
|
| 249 |
</script>
|
| 250 |
"""
|
| 251 |
)
|
|
|
|
| 252 |
# (C) CSS ์คํ์ผ
|
| 253 |
gr.HTML(
|
| 254 |
"""
|
|
|
|
| 3 |
import random
|
| 4 |
import os
|
| 5 |
import threading
|
| 6 |
+
import pandas as pd
|
| 7 |
from datasets import load_dataset
|
| 8 |
|
| 9 |
+
# CSV ํ์ผ ๊ฒฝ๋ก์ ๋์ ์ ๊ทผ์ ์ํ Lock ์ ์ธ
|
| 10 |
+
DATA_FILE = "global_data.csv"
|
| 11 |
+
data_lock = threading.Lock()
|
| 12 |
|
| 13 |
+
def initialize_global_data():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
"""
|
| 15 |
+
DATA_FILE์ด ์กด์ฌํ์ง ์์ผ๋ฉด, gaeunseo/Taskmaster_sample_data ๋ฐ์ดํฐ์
์ ๋ก๋ํ์ฌ DataFrame์ผ๋ก ๋ณํํ ํ CSV ํ์ผ๋ก ์ ์ฅํฉ๋๋ค.
|
| 16 |
+
์ด๋ฏธ ํ์ผ์ด ์์ผ๋ฉด ํ์ผ์์ ๋ฐ์ดํฐ๋ฅผ ์ฝ์ด DataFrame์ ๋ฐํํฉ๋๋ค.
|
| 17 |
"""
|
| 18 |
+
if not os.path.exists(DATA_FILE):
|
| 19 |
+
ds = load_dataset("gaeunseo/Taskmaster_sample_data", split="train")
|
| 20 |
+
data = ds.to_pandas()
|
| 21 |
+
# ํ์ํ ์ปฌ๋ผ์ด ์์ผ๋ฉด ์ถ๊ฐํฉ๋๋ค.
|
| 22 |
+
if "used" not in data.columns:
|
| 23 |
+
data["used"] = False
|
| 24 |
+
if "overlapping" not in data.columns:
|
| 25 |
+
data["overlapping"] = ""
|
| 26 |
+
if "text" not in data.columns:
|
| 27 |
+
data["text"] = ""
|
| 28 |
+
data.to_csv(DATA_FILE, index=False)
|
| 29 |
+
return data
|
| 30 |
+
else:
|
| 31 |
+
with data_lock:
|
| 32 |
+
df = pd.read_csv(DATA_FILE)
|
| 33 |
+
return df
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
+
def load_global_data():
|
| 36 |
+
"""CSV ํ์ผ์์ global_data DataFrame์ ์ฝ์ด์ต๋๋ค."""
|
| 37 |
+
with data_lock:
|
| 38 |
+
df = pd.read_csv(DATA_FILE)
|
| 39 |
+
return df
|
| 40 |
|
| 41 |
+
def save_global_data(df):
|
| 42 |
+
"""DataFrame์ CSV ํ์ผ์ ์ ์ฅํฉ๋๋ค."""
|
| 43 |
+
with data_lock:
|
| 44 |
+
df.to_csv(DATA_FILE, index=False)
|
| 45 |
+
|
| 46 |
+
# CSV ํ์ผ์ ์ ์ฅ๋ global_data ์ด๊ธฐํ
|
| 47 |
+
global_data = initialize_global_data()
|
| 48 |
|
| 49 |
def get_random_row_from_dataset():
|
| 50 |
"""
|
| 51 |
+
CSV ํ์ผ์ ์ ์ฅ๋ global_data์์,
|
| 52 |
+
1. conversation_id๋ณ๋ก ๊ทธ๋ฃนํํ๊ณ ,
|
| 53 |
+
2. ๊ฐ ๊ทธ๋ฃน์์ ๋ชจ๋ ํ์ used ์ปฌ๋ผ์ด False์ด๋ฉฐ, ๊ทธ๋ฃน ๋ด์ overlapping ์ปฌ๋ผ์ด "TT"์ธ ํ์ด ์กด์ฌํ๋ ๊ทธ๋ฃน๋ง valid๋ก ๊ฐ์ฃผํฉ๋๋ค.
|
| 54 |
+
validํ ๊ทธ๋ฃน๏ฟฝ๏ฟฝ ์ค ๋๋คํ๊ฒ ํ๋์ ๊ทธ๋ฃน์ ์ ํํ ํ,
|
| 55 |
+
- ํด๋น ๊ทธ๋ฃน์ ๋ชจ๋ ํ์ used ๊ฐ์ True๋ก ์
๋ฐ์ดํธ(์ฆ, ์ ์ฒด ๊ทธ๋ฃน์ ํ ๋น)ํ๊ณ CSV ํ์ผ์ ์ ์ฅํฉ๋๋ค.
|
| 56 |
+
- ์ ํ๋ ๊ทธ๋ฃน ๋ด์์ overlapping ์ปฌ๋ผ์ด "TT"์ธ ํ(์ฌ๋ฌ ๊ฐ๋ผ๋ฉด ์ฒซ ๋ฒ์งธ)์ ๋ฐํํฉ๋๋ค.
|
| 57 |
"""
|
| 58 |
+
global global_data
|
| 59 |
+
global_data = load_global_data() # ์ต์ ๋ฐ์ดํฐ ๋ก๋
|
| 60 |
+
groups = global_data.groupby('conversation_id')
|
| 61 |
+
valid_groups = []
|
| 62 |
+
for cid, group in groups:
|
| 63 |
+
# ๋ชจ๋ ํ์ used ๊ฐ์ด False์ด๊ณ , ๊ทธ๋ฃน ๋ด์ overlapping ๊ฐ์ด "TT"์ธ ํ์ด ์๋ ๊ทธ๋ฃน ํํฐ๋ง
|
| 64 |
+
if group['used'].apply(lambda x: bool(x) == False).all() and (group['overlapping'] == "TT").any():
|
| 65 |
+
valid_groups.append((cid, group))
|
| 66 |
+
if not valid_groups:
|
| 67 |
+
return None
|
| 68 |
+
chosen_cid, chosen_group = random.choice(valid_groups)
|
| 69 |
+
# ์ ์ฒด ๊ทธ๋ฃน์ used ๊ฐ์ True๋ก ์
๋ฐ์ดํธ
|
| 70 |
+
global_data.loc[global_data['conversation_id'] == chosen_cid, 'used'] = True
|
| 71 |
+
save_global_data(global_data)
|
| 72 |
+
# ์ ํ๋ ๊ทธ๋ฃน ๋ด์์ overlapping ์ปฌ๋ผ์ด "TT"์ธ ํ์ ๋ฐํ (์ฌ๋ฌ ๊ฐ๋ผ๋ฉด ์ฒซ ๋ฒ์งธ)
|
| 73 |
+
chosen_rows = chosen_group[chosen_group['overlapping'] == "TT"]
|
| 74 |
+
if chosen_rows.empty:
|
| 75 |
+
return None
|
| 76 |
+
chosen_row = chosen_rows.iloc[0]
|
| 77 |
+
return chosen_row.to_dict()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
|
| 79 |
+
# --- ์ด๊ธฐ ๋ํ ๋ถ๋ฌ์ค๊ธฐ ---
|
| 80 |
+
# ๋ฐ์ดํฐ์
์ text ์ปฌ๋ผ์ "[turn]"์ ๊ธฐ์ค์ผ๋ก ๋ํ๊ฐ ๊ตฌ๋ถ๋์ด ์๋ค๊ณ ๊ฐ์ ํฉ๋๋ค.
|
| 81 |
+
row = get_random_row_from_dataset()
|
| 82 |
+
if row is None:
|
| 83 |
human_message = "No valid conversation available."
|
| 84 |
ai_message = "No valid conversation available."
|
| 85 |
else:
|
| 86 |
+
raw_text = row['text']
|
| 87 |
+
human_message = raw_text.split("[turn]")[0].strip()
|
| 88 |
+
ai_message = raw_text.split("[turn]")[1].strip()
|
| 89 |
|
| 90 |
#############################################
|
| 91 |
+
# ์ฑํ
์ธํฐํ์ด์ค ๊ด๋ จ ํจ์ (๋งํ์ , ํ์ดํ ํจ๊ณผ, ํธ์ง ๊ธฐ๋ฅ)
|
| 92 |
#############################################
|
| 93 |
|
| 94 |
def get_initial_human_html():
|
|
|
|
| 117 |
bubble_end = "</div>"
|
| 118 |
emoji_html = "<div class='emoji'>๐ง</div>"
|
| 119 |
wrapper_end = "</div>"
|
| 120 |
+
|
| 121 |
+
# ์ด๊ธฐ ์ํ: ๋น ๋งํ์ ๊ณผ ์ด๋ชจํฐ์ฝ
|
| 122 |
yield wrapper_start + bubble_start + bubble_end + emoji_html + wrapper_end
|
| 123 |
+
|
| 124 |
+
# human_message๋ฅผ ํ ๊ธ์์ฉ ์ถ๊ฐ (ํ์ดํ ํจ๊ณผ)
|
| 125 |
for i, ch in enumerate(human_message):
|
| 126 |
+
bubble_content += f"<span data-index='{i}'>{ch}</span>"
|
| 127 |
+
current_html = wrapper_start + bubble_start + bubble_content + bubble_end + emoji_html + wrapper_end
|
| 128 |
+
yield current_html
|
| 129 |
+
time.sleep(0.05)
|
| 130 |
|
| 131 |
def submit_edit(edited_text):
|
| 132 |
"""
|
| 133 |
Submit ๋ฒํผ ํด๋ฆญ ์ ํธ์ถ๋๋ ํจ์.
|
| 134 |
+
1. ํธ์ง๋ human ๋ฉ์์ง(โ๏ธ ์๋ถ๋ถ)๋ฅผ ์ ํ์ผ๋ก global_data์ ์ถ๊ฐํฉ๋๋ค.
|
| 135 |
2. get_random_row_from_dataset()์ ํตํด ์๋ก์ด ๋ํ๋ฅผ ๊ฐ์ ธ์ค๊ณ , ์ ์ญ ๋ณ์ human_message์ ai_message๋ฅผ ์
๋ฐ์ดํธํฉ๋๋ค.
|
| 136 |
3. ์ด๊ธฐ ์ํ์ human ๋งํ์ ๊ณผ ai ๋งํ์ HTML์ ๋ฐํํ์ฌ ์ธํฐํ์ด์ค๋ฅผ ๋ฆฌ์
ํฉ๋๋ค.
|
| 137 |
"""
|
| 138 |
+
global human_message, ai_message
|
| 139 |
+
data = load_global_data()
|
| 140 |
+
new_row = {
|
| 141 |
+
"conversation_id": "edited_" + str(random.randint(1000, 9999)),
|
| 142 |
+
"used": False,
|
| 143 |
+
"overlapping": "",
|
| 144 |
+
"text": edited_text,
|
| 145 |
+
"human_message": edited_text,
|
| 146 |
+
"ai_message": ""
|
| 147 |
+
}
|
| 148 |
+
new_df = pd.DataFrame([new_row])
|
| 149 |
+
data = pd.concat([data, new_df], ignore_index=True)
|
| 150 |
+
save_global_data(data)
|
| 151 |
+
|
| 152 |
+
new_row_data = get_random_row_from_dataset()
|
| 153 |
+
if new_row_data is None:
|
| 154 |
+
human_message = "No valid conversation available."
|
| 155 |
+
ai_message = "No valid conversation available."
|
| 156 |
+
else:
|
| 157 |
+
raw_text = new_row_data['text']
|
| 158 |
+
human_message = raw_text.split("[turn]")[0].strip()
|
| 159 |
+
ai_message = raw_text.split("[turn]")[1].strip()
|
| 160 |
+
|
|
|
|
| 161 |
new_human_html = get_initial_human_html()
|
| 162 |
new_ai_html = f"""
|
| 163 |
<div class="ai-wrapper" style="display: flex; align-items: flex-end; justify-content: flex-start; gap: 5px; width: 100%;">
|
|
|
|
| 168 |
return new_human_html, new_ai_html
|
| 169 |
|
| 170 |
#############################################
|
| 171 |
+
# Gradio ์ธํฐํ์ด์ค ๊ตฌ์ฑ
|
| 172 |
#############################################
|
| 173 |
|
| 174 |
with gr.Blocks() as demo:
|
| 175 |
+
# (A) ํ์ด์ง ์๋จ ์คํฌ๋ฆฝํธ: Human ๋งํ์ ๋ด์ ๊ฐ <span data-index="...">๋ฅผ ํด๋ฆญํ๋ฉด,
|
| 176 |
# ํด๋น ์์น์ โ๏ธ ์์ด์ฝ์ด ์ฝ์
๋๊ณ , ๊ทธ ์ดํ ํ
์คํธ๊ฐ ํ์์ผ๋ก ๋ณ๊ฒฝ๋ฉ๋๋ค.
|
| 177 |
gr.HTML(
|
| 178 |
"""
|
|
|
|
| 201 |
</script>
|
| 202 |
"""
|
| 203 |
)
|
| 204 |
+
|
| 205 |
+
# (B) ์ถ๊ฐ ์คํฌ๋ฆฝํธ: Submit ๋ฒํผ ํด๋ฆญ ์, human_message div์ innerText์์ "โ๏ธ"๋ฅผ ๊ธฐ์ค์ผ๋ก ํธ์ง๋ ํ
์คํธ(์๋ถ๋ถ)๋ฅผ ์จ๊น ํ
์คํธ๋ฐ์ค์ ์
๋ฐ์ดํธ
|
| 206 |
gr.HTML(
|
| 207 |
"""
|
| 208 |
<script>
|
|
|
|
| 221 |
</script>
|
| 222 |
"""
|
| 223 |
)
|
| 224 |
+
|
| 225 |
# (C) CSS ์คํ์ผ
|
| 226 |
gr.HTML(
|
| 227 |
"""
|