Update app.py
Browse files
app.py
CHANGED
@@ -3,6 +3,7 @@ from huggingface_hub import InferenceClient
|
|
3 |
import os
|
4 |
import pandas as pd
|
5 |
from typing import List, Dict, Tuple
|
|
|
6 |
|
7 |
# μΆλ‘ API ν΄λΌμ΄μΈνΈ μ€μ
|
8 |
hf_client = InferenceClient(
|
@@ -27,11 +28,6 @@ def load_parquet(filename: str) -> str:
|
|
27 |
except Exception as e:
|
28 |
return f"νμΌμ μ½λ μ€ μ€λ₯κ° λ°μνμ΅λλ€: {str(e)}"
|
29 |
|
30 |
-
# μ½λ νμΌ λ‘λ
|
31 |
-
fashion_code = load_code('fashion.cod')
|
32 |
-
uhdimage_code = load_code('uhdimage.cod')
|
33 |
-
MixGEN_code = load_code('mgen.cod')
|
34 |
-
|
35 |
def respond(
|
36 |
message: str,
|
37 |
history: List[Dict[str, str]],
|
@@ -42,19 +38,23 @@ def respond(
|
|
42 |
parquet_data: Dict = None
|
43 |
) -> str:
|
44 |
# μμ€ν
ν둬ννΈ μ€μ
|
45 |
-
system_prefix = """λ°λμ νκΈλ‘ λ΅λ³ν κ². λλ μ£Όμ΄μ§ λ°μ΄ν°λ₯Ό κΈ°λ°μΌλ‘ "μλΉμ€ μ¬μ© μ€λͺ
λ° μλ΄, Q&Aλ₯Ό νλ μν μ΄λ€". μμ£Ό μΉμ νκ³ μμΈνκ² Markdown νμμΌλ‘ μμ±νλΌ. λλ λ°μ΄ν°λ₯Ό κΈ°λ°μΌλ‘ μ¬μ© μ€λͺ
λ° μ§μ μλ΅μ μ§ννλ©°, μ΄μ©μμκ² λμμ μ£Όμ΄μΌ νλ€. μ΄μ©μκ° κΆκΈν΄ν λ§ν λ΄μ©μ μΉμ νκ² μλ €μ£Όλλ‘ νλΌ. λ°μ΄ν° μ 체 λ΄μ©μ λν΄μλ 보μμ μ μ§νκ³ , λ―Όκ°ν μ 보λ 곡κ°νμ§ λ§λΌ."""
|
46 |
-
|
47 |
-
# Parquet λ°μ΄ν° ν¬ν¨
|
48 |
if parquet_data:
|
|
|
|
|
|
|
|
|
49 |
df = pd.read_json(parquet_data)
|
50 |
-
|
51 |
-
|
|
|
|
|
|
|
|
|
52 |
|
53 |
# μμ€ν
λ©μμ§μ λν κΈ°λ‘ κ²°ν©
|
54 |
messages = [{"role": "system", "content": system_prefix}]
|
55 |
for chat in history:
|
56 |
messages.append({"role": chat['role'], "content": chat['content']})
|
57 |
-
# λ§μ§λ§ λ©μμ§λ μ΄λ―Έ historyμ μμΌλ―λ‘ μΆκ°νμ§ μμ΅λλ€.
|
58 |
|
59 |
try:
|
60 |
# λͺ¨λΈμ λ©μμ§ μ μ‘ λ° μλ΅ λ°κΈ°
|
@@ -66,7 +66,7 @@ def respond(
|
|
66 |
temperature=temperature,
|
67 |
top_p=top_p,
|
68 |
):
|
69 |
-
token = msg.
|
70 |
if token:
|
71 |
response += token
|
72 |
yield response
|
@@ -101,8 +101,8 @@ def upload_parquet(file_path: str) -> Tuple[str, str, str]:
|
|
101 |
df = pd.read_parquet(file_path, engine='pyarrow')
|
102 |
# MarkdownμΌλ‘ λ³ννμ¬ λ―Έλ¦¬λ³΄κΈ°
|
103 |
parquet_content = df.head(10).to_markdown(index=False)
|
104 |
-
# DataFrameμ JSON
|
105 |
-
parquet_json = df.to_json()
|
106 |
return "Parquet νμΌμ΄ μ±κ³΅μ μΌλ‘ μ
λ‘λλμμ΅λλ€.", parquet_content, parquet_json
|
107 |
except Exception as e:
|
108 |
return f"Parquet νμΌ μ
λ‘λ μ€ μ€λ₯κ° λ°μνμ΅λλ€: {str(e)}", "", ""
|
@@ -113,7 +113,7 @@ def text_to_parquet(text: str) -> Tuple[str, str, str]:
|
|
113 |
data = [line.split(',') for line in text.strip().split('\n')]
|
114 |
df = pd.DataFrame(data, columns=['id', 'text', 'label', 'metadata'])
|
115 |
# λ°μ΄ν° μ ν μ΅μ ν
|
116 |
-
df = df.astype({'id': 'int32', 'text': 'string', 'label': '
|
117 |
# Parquet νμΌλ‘ λ³ν
|
118 |
parquet_filename = 'text_to_parquet.parquet'
|
119 |
df.to_parquet(parquet_filename, engine='pyarrow', compression='snappy')
|
@@ -173,6 +173,8 @@ with gr.Blocks(css=css) as demo:
|
|
173 |
temperature = gr.Slider(minimum=0, maximum=1, value=0.7, label="Temperature")
|
174 |
top_p = gr.Slider(minimum=0, maximum=1, value=0.9, label="Top P")
|
175 |
|
|
|
|
|
176 |
def handle_message_data_upload(
|
177 |
message: str,
|
178 |
history: List[Dict[str, str]],
|
@@ -214,7 +216,7 @@ with gr.Blocks(css=css) as demo:
|
|
214 |
max_tokens,
|
215 |
temperature,
|
216 |
top_p,
|
217 |
-
|
218 |
],
|
219 |
outputs=[chatbot_data_upload, msg_data_upload],
|
220 |
queue=True
|
@@ -245,7 +247,6 @@ with gr.Blocks(css=css) as demo:
|
|
245 |
parquet_upload_button = gr.Button("μ
λ‘λ")
|
246 |
parquet_upload_status = gr.Textbox(label="μ
λ‘λ μν", interactive=False)
|
247 |
parquet_preview_chat = gr.Markdown(label="Parquet νμΌ λ―Έλ¦¬λ³΄κΈ°")
|
248 |
-
parquet_data_state = gr.State()
|
249 |
|
250 |
def handle_parquet_upload(file_path: str):
|
251 |
message, parquet_content, parquet_json = upload_parquet(file_path)
|
@@ -293,7 +294,7 @@ with gr.Blocks(css=css) as demo:
|
|
293 |
text_input = gr.Textbox(
|
294 |
label="ν
μ€νΈ μ
λ ₯ (κ° νμ `id,text,label,metadata` νμμΌλ‘ μ
λ ₯)",
|
295 |
lines=10,
|
296 |
-
placeholder="μ: 1
|
297 |
)
|
298 |
convert_button = gr.Button("λ³ν λ° λ€μ΄λ‘λ")
|
299 |
convert_status = gr.Textbox(label="λ³ν μν", interactive=False)
|
@@ -317,3 +318,4 @@ with gr.Blocks(css=css) as demo:
|
|
317 |
|
318 |
if __name__ == "__main__":
|
319 |
demo.launch()
|
|
|
|
3 |
import os
|
4 |
import pandas as pd
|
5 |
from typing import List, Dict, Tuple
|
6 |
+
import json
|
7 |
|
8 |
# μΆλ‘ API ν΄λΌμ΄μΈνΈ μ€μ
|
9 |
hf_client = InferenceClient(
|
|
|
28 |
except Exception as e:
|
29 |
return f"νμΌμ μ½λ μ€ μ€λ₯κ° λ°μνμ΅λλ€: {str(e)}"
|
30 |
|
|
|
|
|
|
|
|
|
|
|
31 |
def respond(
|
32 |
message: str,
|
33 |
history: List[Dict[str, str]],
|
|
|
38 |
parquet_data: Dict = None
|
39 |
) -> str:
|
40 |
# μμ€ν
ν둬ννΈ μ€μ
|
|
|
|
|
|
|
41 |
if parquet_data:
|
42 |
+
# Parquet λ°μ΄ν°κ° μ
λ‘λλ κ²½μ°
|
43 |
+
system_prefix = """λ°λμ νκΈλ‘ λ΅λ³ν κ². λλ μ
λ‘λλ λ°μ΄ν°λ₯Ό κΈ°λ°μΌλ‘ μ§λ¬Έμ λ΅λ³νλ μν μ νλ€. λ°μ΄ν°λ₯Ό λΆμνμ¬ μ¬μ©μμκ² λμμ΄ λλ μ 보λ₯Ό μ 곡νλΌ. λ°μ΄ν°λ₯Ό νμ©νμ¬ μμΈνκ³ μ νν λ΅λ³μ μ 곡νλ, λ―Όκ°ν μ 보λ κ°μΈ μ 보λ₯Ό λ
ΈμΆνμ§ λ§λΌ."""
|
44 |
+
# Parquet λ°μ΄ν°λ₯Ό λ¬Έμμ΄λ‘ λ³ννμ¬ μμ€ν
λ©μμ§μ ν¬ν¨
|
45 |
+
# λ°μ΄ν°κ° λ무 ν΄ κ²½μ° ν ν° μ νμ΄ μμΌλ―λ‘, μ£Όμ λ΄μ©μ μμ½νμ¬ ν¬ν¨νκ±°λ λ°μ΄ν°μ ꡬ쑰μ νΉμ§μ μ€λͺ
νλλ‘ νλ€.
|
46 |
df = pd.read_json(parquet_data)
|
47 |
+
# λ°μ΄ν°μ μμ½ μ 보 μμ±
|
48 |
+
data_summary = df.describe(include='all').to_string()
|
49 |
+
system_prefix += f"\n\nμ
λ‘λλ λ°μ΄ν°μ μμ½ μ 보:\n{data_summary}"
|
50 |
+
else:
|
51 |
+
# Parquet λ°μ΄ν°κ° μ
λ‘λλμ§ μμ κ²½μ°
|
52 |
+
system_prefix = system_message or "λλ AI μ‘°μΈμ μν μ΄λ€."
|
53 |
|
54 |
# μμ€ν
λ©μμ§μ λν κΈ°λ‘ κ²°ν©
|
55 |
messages = [{"role": "system", "content": system_prefix}]
|
56 |
for chat in history:
|
57 |
messages.append({"role": chat['role'], "content": chat['content']})
|
|
|
58 |
|
59 |
try:
|
60 |
# λͺ¨λΈμ λ©μμ§ μ μ‘ λ° μλ΅ λ°κΈ°
|
|
|
66 |
temperature=temperature,
|
67 |
top_p=top_p,
|
68 |
):
|
69 |
+
token = msg.get('delta', {}).get('content', '')
|
70 |
if token:
|
71 |
response += token
|
72 |
yield response
|
|
|
101 |
df = pd.read_parquet(file_path, engine='pyarrow')
|
102 |
# MarkdownμΌλ‘ λ³ννμ¬ λ―Έλ¦¬λ³΄κΈ°
|
103 |
parquet_content = df.head(10).to_markdown(index=False)
|
104 |
+
# DataFrameμ JSON λ¬Έμμ΄λ‘ λ³ν
|
105 |
+
parquet_json = df.to_json(orient='records', force_ascii=False)
|
106 |
return "Parquet νμΌμ΄ μ±κ³΅μ μΌλ‘ μ
λ‘λλμμ΅λλ€.", parquet_content, parquet_json
|
107 |
except Exception as e:
|
108 |
return f"Parquet νμΌ μ
λ‘λ μ€ μ€λ₯κ° λ°μνμ΅λλ€: {str(e)}", "", ""
|
|
|
113 |
data = [line.split(',') for line in text.strip().split('\n')]
|
114 |
df = pd.DataFrame(data, columns=['id', 'text', 'label', 'metadata'])
|
115 |
# λ°μ΄ν° μ ν μ΅μ ν
|
116 |
+
df = df.astype({'id': 'int32', 'text': 'string', 'label': 'string', 'metadata': 'string'})
|
117 |
# Parquet νμΌλ‘ λ³ν
|
118 |
parquet_filename = 'text_to_parquet.parquet'
|
119 |
df.to_parquet(parquet_filename, engine='pyarrow', compression='snappy')
|
|
|
173 |
temperature = gr.Slider(minimum=0, maximum=1, value=0.7, label="Temperature")
|
174 |
top_p = gr.Slider(minimum=0, maximum=1, value=0.9, label="Top P")
|
175 |
|
176 |
+
parquet_data_state = gr.State()
|
177 |
+
|
178 |
def handle_message_data_upload(
|
179 |
message: str,
|
180 |
history: List[Dict[str, str]],
|
|
|
216 |
max_tokens,
|
217 |
temperature,
|
218 |
top_p,
|
219 |
+
parquet_data_state, # parquet_data_stateλ₯Ό μ¬μ©νμ¬ μ
λ‘λλ λ°μ΄ν°λ₯Ό μ λ¬
|
220 |
],
|
221 |
outputs=[chatbot_data_upload, msg_data_upload],
|
222 |
queue=True
|
|
|
247 |
parquet_upload_button = gr.Button("μ
λ‘λ")
|
248 |
parquet_upload_status = gr.Textbox(label="μ
λ‘λ μν", interactive=False)
|
249 |
parquet_preview_chat = gr.Markdown(label="Parquet νμΌ λ―Έλ¦¬λ³΄κΈ°")
|
|
|
250 |
|
251 |
def handle_parquet_upload(file_path: str):
|
252 |
message, parquet_content, parquet_json = upload_parquet(file_path)
|
|
|
294 |
text_input = gr.Textbox(
|
295 |
label="ν
μ€νΈ μ
λ ₯ (κ° νμ `id,text,label,metadata` νμμΌλ‘ μ
λ ₯)",
|
296 |
lines=10,
|
297 |
+
placeholder="μ: 1,μ΄μμ ,μ₯κ΅°,κ±°λΆμ \n2,μκ· ,μ₯κ΅°,λͺ¨ν¨\n3,μ μ‘°,μ,μκΈ°\n4,λμν λ―Έ νλ°μμ,μ,μΉ¨λ΅"
|
298 |
)
|
299 |
convert_button = gr.Button("λ³ν λ° λ€μ΄λ‘λ")
|
300 |
convert_status = gr.Textbox(label="λ³ν μν", interactive=False)
|
|
|
318 |
|
319 |
if __name__ == "__main__":
|
320 |
demo.launch()
|
321 |
+
|