Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -8,10 +8,15 @@ import random
|
|
| 8 |
from datasets import load_dataset
|
| 9 |
import numpy as np
|
| 10 |
from sklearn.feature_extraction.text import TfidfVectorizer
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
# GPU λ©λͺ¨λ¦¬ κ΄λ¦¬
|
| 13 |
torch.cuda.empty_cache()
|
| 14 |
|
|
|
|
| 15 |
HF_TOKEN = os.environ.get("HF_TOKEN", None)
|
| 16 |
MODEL_ID = "CohereForAI/c4ai-command-r7b-12-2024"
|
| 17 |
MODELS = os.environ.get("MODELS")
|
|
@@ -36,6 +41,64 @@ vectorizer = TfidfVectorizer(max_features=1000)
|
|
| 36 |
question_vectors = vectorizer.fit_transform(questions)
|
| 37 |
print("TF-IDF 벑ν°ν μλ£")
|
| 38 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
def find_relevant_context(query, top_k=3):
|
| 40 |
# 쿼리 벑ν°ν
|
| 41 |
query_vector = vectorizer.transform([query])
|
|
@@ -49,7 +112,7 @@ def find_relevant_context(query, top_k=3):
|
|
| 49 |
# κ΄λ ¨ 컨ν
μ€νΈ μΆμΆ
|
| 50 |
relevant_contexts = []
|
| 51 |
for idx in top_indices:
|
| 52 |
-
if similarities[idx] > 0:
|
| 53 |
relevant_contexts.append({
|
| 54 |
'question': questions[idx],
|
| 55 |
'answer': wiki_dataset['train']['answer'][idx],
|
|
@@ -58,16 +121,94 @@ def find_relevant_context(query, top_k=3):
|
|
| 58 |
|
| 59 |
return relevant_contexts
|
| 60 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
@spaces.GPU
|
| 62 |
-
def stream_chat(message: str, history: list, temperature: float, max_new_tokens: int, top_p: float, top_k: int, penalty: float):
|
| 63 |
print(f'message is - {message}')
|
| 64 |
print(f'history is - {history}')
|
| 65 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
# κ΄λ ¨ 컨ν
μ€νΈ μ°ΎκΈ°
|
| 67 |
relevant_contexts = find_relevant_context(message)
|
| 68 |
-
|
| 69 |
for ctx in relevant_contexts:
|
| 70 |
-
|
| 71 |
|
| 72 |
# λν νμ€ν 리 ꡬμ±
|
| 73 |
conversation = []
|
|
@@ -76,15 +217,13 @@ def stream_chat(message: str, history: list, temperature: float, max_new_tokens:
|
|
| 76 |
{"role": "user", "content": prompt},
|
| 77 |
{"role": "assistant", "content": answer}
|
| 78 |
])
|
| 79 |
-
|
| 80 |
|
| 81 |
-
#
|
| 82 |
-
final_message =
|
| 83 |
conversation.append({"role": "user", "content": final_message})
|
| 84 |
|
| 85 |
input_ids = tokenizer.apply_chat_template(conversation, tokenize=False, add_generation_prompt=True)
|
| 86 |
inputs = tokenizer(input_ids, return_tensors="pt").to(0)
|
| 87 |
-
|
| 88 |
|
| 89 |
streamer = TextIteratorStreamer(tokenizer, timeout=10., skip_prompt=True, skip_special_tokens=True)
|
| 90 |
|
|
@@ -108,8 +247,6 @@ def stream_chat(message: str, history: list, temperature: float, max_new_tokens:
|
|
| 108 |
buffer += new_text
|
| 109 |
yield buffer
|
| 110 |
|
| 111 |
-
chatbot = gr.Chatbot(height=500)
|
| 112 |
-
|
| 113 |
CSS = """
|
| 114 |
/* μ 체 νμ΄μ§ μ€νμΌλ§ */
|
| 115 |
body {
|
|
@@ -117,131 +254,72 @@ body {
|
|
| 117 |
min-height: 100vh;
|
| 118 |
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
|
| 119 |
}
|
| 120 |
-
/*
|
| 121 |
-
.container {
|
| 122 |
-
max-width: 1200px;
|
| 123 |
-
margin: 0 auto;
|
| 124 |
-
padding: 2rem;
|
| 125 |
-
background: rgba(255, 255, 255, 0.95);
|
| 126 |
-
border-radius: 20px;
|
| 127 |
-
box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
|
| 128 |
-
backdrop-filter: blur(10px);
|
| 129 |
-
transform: perspective(1000px) translateZ(0);
|
| 130 |
-
transition: all 0.3s ease;
|
| 131 |
-
}
|
| 132 |
-
/* μ λͺ© μ€νμΌλ§ */
|
| 133 |
-
h1 {
|
| 134 |
-
color: #2d3436;
|
| 135 |
-
font-size: 2.5rem;
|
| 136 |
-
text-align: center;
|
| 137 |
-
margin-bottom: 2rem;
|
| 138 |
-
text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.1);
|
| 139 |
-
transform: perspective(1000px) translateZ(20px);
|
| 140 |
-
}
|
| 141 |
-
h3 {
|
| 142 |
-
text-align: center;
|
| 143 |
-
color: #2d3436;
|
| 144 |
-
font-size: 1.5rem;
|
| 145 |
-
margin: 1rem 0;
|
| 146 |
-
}
|
| 147 |
-
/* μ±ν
λ°μ€ μ€νμΌλ§ */
|
| 148 |
-
.chatbox {
|
| 149 |
-
background: white;
|
| 150 |
-
border-radius: 15px;
|
| 151 |
-
box-shadow: 0 8px 32px rgba(31, 38, 135, 0.15);
|
| 152 |
-
backdrop-filter: blur(4px);
|
| 153 |
-
border: 1px solid rgba(255, 255, 255, 0.18);
|
| 154 |
-
padding: 1rem;
|
| 155 |
-
margin: 1rem 0;
|
| 156 |
-
transform: translateZ(0);
|
| 157 |
-
transition: all 0.3s ease;
|
| 158 |
-
}
|
| 159 |
-
/* λ©μμ§ μ€νμΌλ§ */
|
| 160 |
-
.chatbox .messages .message.user {
|
| 161 |
-
background: linear-gradient(145deg, #e1f5fe, #bbdefb);
|
| 162 |
-
border-radius: 15px;
|
| 163 |
-
padding: 1rem;
|
| 164 |
-
margin: 0.5rem;
|
| 165 |
-
box-shadow: 5px 5px 15px rgba(0, 0, 0, 0.05);
|
| 166 |
-
transform: translateZ(10px);
|
| 167 |
-
animation: messageIn 0.3s ease-out;
|
| 168 |
-
}
|
| 169 |
-
.chatbox .messages .message.bot {
|
| 170 |
-
background: linear-gradient(145deg, #f5f5f5, #eeeeee);
|
| 171 |
-
border-radius: 15px;
|
| 172 |
-
padding: 1rem;
|
| 173 |
-
margin: 0.5rem;
|
| 174 |
-
box-shadow: 5px 5px 15px rgba(0, 0, 0, 0.05);
|
| 175 |
-
transform: translateZ(10px);
|
| 176 |
-
animation: messageIn 0.3s ease-out;
|
| 177 |
-
}
|
| 178 |
-
/* λ²νΌ μ€νμΌλ§ */
|
| 179 |
-
.duplicate-button {
|
| 180 |
-
background: linear-gradient(145deg, #24292e, #1a1e22) !important;
|
| 181 |
-
color: white !important;
|
| 182 |
-
border-radius: 100vh !important;
|
| 183 |
-
padding: 0.8rem 1.5rem !important;
|
| 184 |
-
box-shadow: 3px 3px 10px rgba(0, 0, 0, 0.2) !important;
|
| 185 |
-
transition: all 0.3s ease !important;
|
| 186 |
-
border: none !important;
|
| 187 |
-
cursor: pointer !important;
|
| 188 |
-
}
|
| 189 |
-
.duplicate-button:hover {
|
| 190 |
-
transform: translateY(-2px) !important;
|
| 191 |
-
box-shadow: 0 5px 15px rgba(0, 0, 0, 0.3) !important;
|
| 192 |
-
}
|
| 193 |
-
/* μ
λ ₯ νλ μ€νμΌλ§ */
|
| 194 |
"""
|
| 195 |
|
| 196 |
with gr.Blocks(css=CSS) as demo:
|
| 197 |
-
gr.
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
gr.
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 245 |
examples=[
|
| 246 |
["νκ΅μ μ ν΅ μ κΈ°μ 24μ κΈ°μ λν΄ μμΈν μ€λͺ
ν΄μ£ΌμΈμ."],
|
| 247 |
["μ°λ¦¬λλΌ μ ν΅ μμ μ€ κ±΄κ°μ μ’μ λ°ν¨μμ 5κ°μ§λ₯Ό μΆμ²νκ³ κ·Έ ν¨λ₯μ μ€λͺ
ν΄μ£ΌμΈμ."],
|
|
@@ -256,8 +334,31 @@ with gr.Blocks(css=CSS) as demo:
|
|
| 256 |
["νκ΅μ μ ν΅ μλ³΅μΈ ν볡μ ꡬ쑰μ νΉμ§μ κ³Όνμ , λ―Ένμ κ΄μ μμ λΆμν΄μ£ΌμΈμ."],
|
| 257 |
["νκ΅μ μ ν΅ κ°μ₯ ꡬ쑰λ₯Ό κΈ°νμ νκ²½ κ΄μ μμ λΆμνκ³ , νλ 건μΆμ μ μ©ν μ μλ μμλ₯Ό μ μν΄μ£ΌμΈμ."]
|
| 258 |
],
|
| 259 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 260 |
)
|
| 261 |
|
| 262 |
if __name__ == "__main__":
|
| 263 |
-
demo.launch()
|
|
|
|
| 8 |
from datasets import load_dataset
|
| 9 |
import numpy as np
|
| 10 |
from sklearn.feature_extraction.text import TfidfVectorizer
|
| 11 |
+
import pandas as pd
|
| 12 |
+
from typing import List, Tuple
|
| 13 |
+
import json
|
| 14 |
+
from datetime import datetime
|
| 15 |
|
| 16 |
# GPU λ©λͺ¨λ¦¬ κ΄λ¦¬
|
| 17 |
torch.cuda.empty_cache()
|
| 18 |
|
| 19 |
+
# νκ²½ λ³μ μ€μ
|
| 20 |
HF_TOKEN = os.environ.get("HF_TOKEN", None)
|
| 21 |
MODEL_ID = "CohereForAI/c4ai-command-r7b-12-2024"
|
| 22 |
MODELS = os.environ.get("MODELS")
|
|
|
|
| 41 |
question_vectors = vectorizer.fit_transform(questions)
|
| 42 |
print("TF-IDF 벑ν°ν μλ£")
|
| 43 |
|
| 44 |
+
class ChatHistory:
|
| 45 |
+
def __init__(self):
|
| 46 |
+
self.history = []
|
| 47 |
+
self.history_file = "/tmp/chat_history.json"
|
| 48 |
+
self.load_history()
|
| 49 |
+
|
| 50 |
+
def add_conversation(self, user_msg: str, assistant_msg: str):
|
| 51 |
+
conversation = {
|
| 52 |
+
"timestamp": datetime.now().isoformat(),
|
| 53 |
+
"messages": [
|
| 54 |
+
{"role": "user", "content": user_msg},
|
| 55 |
+
{"role": "assistant", "content": assistant_msg}
|
| 56 |
+
]
|
| 57 |
+
}
|
| 58 |
+
self.history.append(conversation)
|
| 59 |
+
self.save_history()
|
| 60 |
+
|
| 61 |
+
def format_for_display(self):
|
| 62 |
+
formatted = []
|
| 63 |
+
for conv in self.history:
|
| 64 |
+
formatted.append([
|
| 65 |
+
conv["messages"][0]["content"],
|
| 66 |
+
conv["messages"][1]["content"]
|
| 67 |
+
])
|
| 68 |
+
return formatted
|
| 69 |
+
|
| 70 |
+
def get_messages_for_api(self):
|
| 71 |
+
messages = []
|
| 72 |
+
for conv in self.history:
|
| 73 |
+
messages.extend([
|
| 74 |
+
{"role": "user", "content": conv["messages"][0]["content"]},
|
| 75 |
+
{"role": "assistant", "content": conv["messages"][1]["content"]}
|
| 76 |
+
])
|
| 77 |
+
return messages
|
| 78 |
+
|
| 79 |
+
def clear_history(self):
|
| 80 |
+
self.history = []
|
| 81 |
+
self.save_history()
|
| 82 |
+
|
| 83 |
+
def save_history(self):
|
| 84 |
+
try:
|
| 85 |
+
with open(self.history_file, 'w', encoding='utf-8') as f:
|
| 86 |
+
json.dump(self.history, f, ensure_ascii=False, indent=2)
|
| 87 |
+
except Exception as e:
|
| 88 |
+
print(f"νμ€ν 리 μ μ₯ μ€ν¨: {e}")
|
| 89 |
+
|
| 90 |
+
def load_history(self):
|
| 91 |
+
try:
|
| 92 |
+
if os.path.exists(self.history_file):
|
| 93 |
+
with open(self.history_file, 'r', encoding='utf-8') as f:
|
| 94 |
+
self.history = json.load(f)
|
| 95 |
+
except Exception as e:
|
| 96 |
+
print(f"νμ€ν 리 λ‘λ μ€ν¨: {e}")
|
| 97 |
+
self.history = []
|
| 98 |
+
|
| 99 |
+
# μ μ ChatHistory μΈμ€ν΄μ€ μμ±
|
| 100 |
+
chat_history = ChatHistory()
|
| 101 |
+
|
| 102 |
def find_relevant_context(query, top_k=3):
|
| 103 |
# 쿼리 벑ν°ν
|
| 104 |
query_vector = vectorizer.transform([query])
|
|
|
|
| 112 |
# κ΄λ ¨ 컨ν
μ€νΈ μΆμΆ
|
| 113 |
relevant_contexts = []
|
| 114 |
for idx in top_indices:
|
| 115 |
+
if similarities[idx] > 0:
|
| 116 |
relevant_contexts.append({
|
| 117 |
'question': questions[idx],
|
| 118 |
'answer': wiki_dataset['train']['answer'][idx],
|
|
|
|
| 121 |
|
| 122 |
return relevant_contexts
|
| 123 |
|
| 124 |
+
def analyze_file_content(content, file_type):
|
| 125 |
+
"""Analyze file content and return structural summary"""
|
| 126 |
+
if file_type in ['parquet', 'csv']:
|
| 127 |
+
try:
|
| 128 |
+
lines = content.split('\n')
|
| 129 |
+
header = lines[0]
|
| 130 |
+
columns = header.count('|') - 1
|
| 131 |
+
rows = len(lines) - 3
|
| 132 |
+
return f"π λ°μ΄ν°μ
ꡬ쑰: {columns}κ° μ»¬λΌ, {rows}κ° λ°μ΄ν°"
|
| 133 |
+
except:
|
| 134 |
+
return "β λ°μ΄ν°μ
ꡬ쑰 λΆμ μ€ν¨"
|
| 135 |
+
|
| 136 |
+
lines = content.split('\n')
|
| 137 |
+
total_lines = len(lines)
|
| 138 |
+
non_empty_lines = len([line for line in lines if line.strip()])
|
| 139 |
+
|
| 140 |
+
if any(keyword in content.lower() for keyword in ['def ', 'class ', 'import ', 'function']):
|
| 141 |
+
functions = len([line for line in lines if 'def ' in line])
|
| 142 |
+
classes = len([line for line in lines if 'class ' in line])
|
| 143 |
+
imports = len([line for line in lines if 'import ' in line or 'from ' in line])
|
| 144 |
+
return f"π» μ½λ ꡬ쑰: {total_lines}μ€ (ν¨μ: {functions}, ν΄λμ€: {classes}, μν¬νΈ: {imports})"
|
| 145 |
+
|
| 146 |
+
paragraphs = content.count('\n\n') + 1
|
| 147 |
+
words = len(content.split())
|
| 148 |
+
return f"π λ¬Έμ ꡬ쑰: {total_lines}μ€, {paragraphs}λ¨λ½, μ½ {words}λ¨μ΄"
|
| 149 |
+
|
| 150 |
+
def read_uploaded_file(file):
|
| 151 |
+
if file is None:
|
| 152 |
+
return "", ""
|
| 153 |
+
try:
|
| 154 |
+
file_ext = os.path.splitext(file.name)[1].lower()
|
| 155 |
+
|
| 156 |
+
if file_ext == '.parquet':
|
| 157 |
+
df = pd.read_parquet(file.name, engine='pyarrow')
|
| 158 |
+
content = df.head(10).to_markdown(index=False)
|
| 159 |
+
return content, "parquet"
|
| 160 |
+
elif file_ext == '.csv':
|
| 161 |
+
encodings = ['utf-8', 'cp949', 'euc-kr', 'latin1']
|
| 162 |
+
for encoding in encodings:
|
| 163 |
+
try:
|
| 164 |
+
df = pd.read_csv(file.name, encoding=encoding)
|
| 165 |
+
content = f"π λ°μ΄ν° 미리보기:\n{df.head(10).to_markdown(index=False)}\n\n"
|
| 166 |
+
content += f"\nπ λ°μ΄ν° μ 보:\n"
|
| 167 |
+
content += f"- μ 체 ν μ: {len(df)}\n"
|
| 168 |
+
content += f"- μ 체 μ΄ μ: {len(df.columns)}\n"
|
| 169 |
+
content += f"- μ»¬λΌ λͺ©λ‘: {', '.join(df.columns)}\n"
|
| 170 |
+
content += f"\nπ μ»¬λΌ λ°μ΄ν° νμ
:\n"
|
| 171 |
+
for col, dtype in df.dtypes.items():
|
| 172 |
+
content += f"- {col}: {dtype}\n"
|
| 173 |
+
null_counts = df.isnull().sum()
|
| 174 |
+
if null_counts.any():
|
| 175 |
+
content += f"\nβ οΈ κ²°μΈ‘μΉ:\n"
|
| 176 |
+
for col, null_count in null_counts[null_counts > 0].items():
|
| 177 |
+
content += f"- {col}: {null_count}κ° λλ½\n"
|
| 178 |
+
return content, "csv"
|
| 179 |
+
except UnicodeDecodeError:
|
| 180 |
+
continue
|
| 181 |
+
raise UnicodeDecodeError(f"β μ§μλλ μΈμ½λ©μΌλ‘ νμΌμ μ½μ μ μμ΅λλ€ ({', '.join(encodings)})")
|
| 182 |
+
else:
|
| 183 |
+
encodings = ['utf-8', 'cp949', 'euc-kr', 'latin1']
|
| 184 |
+
for encoding in encodings:
|
| 185 |
+
try:
|
| 186 |
+
with open(file.name, 'r', encoding=encoding) as f:
|
| 187 |
+
content = f.read()
|
| 188 |
+
return content, "text"
|
| 189 |
+
except UnicodeDecodeError:
|
| 190 |
+
continue
|
| 191 |
+
raise UnicodeDecodeError(f"β μ§μλλ μΈμ½λ©μΌλ‘ νμΌμ μ½μ μ μμ΅λλ€ ({', '.join(encodings)})")
|
| 192 |
+
except Exception as e:
|
| 193 |
+
return f"β νμΌ μ½κΈ° μ€λ₯: {str(e)}", "error"
|
| 194 |
+
|
| 195 |
@spaces.GPU
|
| 196 |
+
def stream_chat(message: str, history: list, uploaded_file, temperature: float, max_new_tokens: int, top_p: float, top_k: int, penalty: float):
|
| 197 |
print(f'message is - {message}')
|
| 198 |
print(f'history is - {history}')
|
| 199 |
|
| 200 |
+
# νμΌ μ
λ‘λ μ²λ¦¬
|
| 201 |
+
file_context = ""
|
| 202 |
+
if uploaded_file:
|
| 203 |
+
content, file_type = read_uploaded_file(uploaded_file)
|
| 204 |
+
if content:
|
| 205 |
+
file_context = f"\n\nμ
λ‘λλ νμΌ λ΄μ©:\n```\n{content}\n```"
|
| 206 |
+
|
| 207 |
# κ΄λ ¨ 컨ν
μ€νΈ μ°ΎκΈ°
|
| 208 |
relevant_contexts = find_relevant_context(message)
|
| 209 |
+
wiki_context = "\n\nκ΄λ ¨ μν€νΌλμ μ 보:\n"
|
| 210 |
for ctx in relevant_contexts:
|
| 211 |
+
wiki_context += f"Q: {ctx['question']}\nA: {ctx['answer']}\nμ μ¬λ: {ctx['similarity']:.3f}\n\n"
|
| 212 |
|
| 213 |
# λν νμ€ν 리 ꡬμ±
|
| 214 |
conversation = []
|
|
|
|
| 217 |
{"role": "user", "content": prompt},
|
| 218 |
{"role": "assistant", "content": answer}
|
| 219 |
])
|
|
|
|
| 220 |
|
| 221 |
+
# μ΅μ’
ν둬ννΈ κ΅¬μ±
|
| 222 |
+
final_message = file_context + wiki_context + "\nνμ¬ μ§λ¬Έ: " + message
|
| 223 |
conversation.append({"role": "user", "content": final_message})
|
| 224 |
|
| 225 |
input_ids = tokenizer.apply_chat_template(conversation, tokenize=False, add_generation_prompt=True)
|
| 226 |
inputs = tokenizer(input_ids, return_tensors="pt").to(0)
|
|
|
|
| 227 |
|
| 228 |
streamer = TextIteratorStreamer(tokenizer, timeout=10., skip_prompt=True, skip_special_tokens=True)
|
| 229 |
|
|
|
|
| 247 |
buffer += new_text
|
| 248 |
yield buffer
|
| 249 |
|
|
|
|
|
|
|
| 250 |
CSS = """
|
| 251 |
/* μ 체 νμ΄μ§ μ€νμΌλ§ */
|
| 252 |
body {
|
|
|
|
| 254 |
min-height: 100vh;
|
| 255 |
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
|
| 256 |
}
|
| 257 |
+
/* ... (μ΄μ μ CSS μ€νμΌ μ μ§) ... */
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 258 |
"""
|
| 259 |
|
| 260 |
with gr.Blocks(css=CSS) as demo:
|
| 261 |
+
with gr.Row():
|
| 262 |
+
with gr.Column(scale=2):
|
| 263 |
+
chatbot = gr.Chatbot(height=500)
|
| 264 |
+
|
| 265 |
+
msg = gr.Textbox(
|
| 266 |
+
label="λ©μμ§ μ
λ ₯",
|
| 267 |
+
show_label=False,
|
| 268 |
+
placeholder="무μμ΄λ λ¬Όμ΄λ³΄μΈμ... π",
|
| 269 |
+
container=False
|
| 270 |
+
)
|
| 271 |
+
|
| 272 |
+
with gr.Row():
|
| 273 |
+
clear = gr.ClearButton([msg, chatbot], value="λνλ΄μ© μ§μ°κΈ°")
|
| 274 |
+
send = gr.Button("보λ΄κΈ° π€")
|
| 275 |
+
|
| 276 |
+
with gr.Column(scale=1):
|
| 277 |
+
gr.Markdown("### νμΌ μ
λ‘λ π")
|
| 278 |
+
file_upload = gr.File(
|
| 279 |
+
label="νμΌ μ ν",
|
| 280 |
+
file_types=["text", ".csv", ".parquet"],
|
| 281 |
+
type="filepath"
|
| 282 |
+
)
|
| 283 |
+
|
| 284 |
+
with gr.Accordion("κ³ κΈ μ€μ βοΈ", open=False):
|
| 285 |
+
temperature = gr.Slider(
|
| 286 |
+
minimum=0,
|
| 287 |
+
maximum=1,
|
| 288 |
+
step=0.1,
|
| 289 |
+
value=0.8,
|
| 290 |
+
label="μ¨λ",
|
| 291 |
+
)
|
| 292 |
+
max_new_tokens = gr.Slider(
|
| 293 |
+
minimum=128,
|
| 294 |
+
maximum=8000,
|
| 295 |
+
step=1,
|
| 296 |
+
value=4000,
|
| 297 |
+
label="μ΅λ ν ν° μ",
|
| 298 |
+
)
|
| 299 |
+
top_p = gr.Slider(
|
| 300 |
+
minimum=0.0,
|
| 301 |
+
maximum=1.0,
|
| 302 |
+
step=0.1,
|
| 303 |
+
value=0.8,
|
| 304 |
+
label="μμ νλ₯ ",
|
| 305 |
+
)
|
| 306 |
+
top_k = gr.Slider(
|
| 307 |
+
minimum=1,
|
| 308 |
+
maximum=20,
|
| 309 |
+
step=1,
|
| 310 |
+
value=20,
|
| 311 |
+
label="μμ K",
|
| 312 |
+
)
|
| 313 |
+
penalty = gr.Slider(
|
| 314 |
+
minimum=0.0,
|
| 315 |
+
maximum=2.0,
|
| 316 |
+
step=0.1,
|
| 317 |
+
value=1.0,
|
| 318 |
+
label="λ°λ³΅ ν¨λν°",
|
| 319 |
+
)
|
| 320 |
+
|
| 321 |
+
# μμ μ§λ¬Έ
|
| 322 |
+
gr.Examples(
|
| 323 |
examples=[
|
| 324 |
["νκ΅μ μ ν΅ μ κΈ°μ 24μ κΈ°μ λν΄ μμΈν μ€λͺ
ν΄μ£ΌμΈμ."],
|
| 325 |
["μ°λ¦¬λλΌ μ ν΅ μμ μ€ κ±΄κ°μ μ’μ λ°ν¨μμ 5κ°μ§λ₯Ό μΆμ²νκ³ κ·Έ ν¨λ₯μ μ€λͺ
ν΄μ£ΌμΈμ."],
|
|
|
|
| 334 |
["νκ΅μ μ ν΅ μλ³΅μΈ ν볡μ ꡬ쑰μ νΉμ§μ κ³Όνμ , λ―Ένμ κ΄μ μμ λΆμν΄μ£ΌμΈμ."],
|
| 335 |
["νκ΅μ μ ν΅ κ°μ₯ ꡬ쑰λ₯Ό κΈ°νμ νκ²½ κ΄μ μμ λΆμνκ³ , νλ 건μΆμ μ μ©ν μ μλ μμλ₯Ό μ μν΄μ£ΌμΈμ."]
|
| 336 |
],
|
| 337 |
+
inputs=msg,
|
| 338 |
+
)
|
| 339 |
+
|
| 340 |
+
# μ΄λ²€νΈ λ°μΈλ©
|
| 341 |
+
msg.submit(
|
| 342 |
+
stream_chat,
|
| 343 |
+
inputs=[msg, chatbot, file_upload, temperature, max_new_tokens, top_p, top_k, penalty],
|
| 344 |
+
outputs=[msg, chatbot]
|
| 345 |
+
)
|
| 346 |
+
|
| 347 |
+
send.click(
|
| 348 |
+
stream_chat,
|
| 349 |
+
inputs=[msg, chatbot, file_upload, temperature, max_new_tokens, top_p, top_k, penalty],
|
| 350 |
+
outputs=[msg, chatbot]
|
| 351 |
+
)
|
| 352 |
+
|
| 353 |
+
# νμΌ μ
λ‘λμ μλ λΆμ
|
| 354 |
+
file_upload.change(
|
| 355 |
+
lambda: "νμΌ λΆμμ μμν©λλ€...",
|
| 356 |
+
outputs=msg
|
| 357 |
+
).then(
|
| 358 |
+
stream_chat,
|
| 359 |
+
inputs=[msg, chatbot, file_upload, temperature, max_new_tokens, top_p, top_k, penalty],
|
| 360 |
+
outputs=[msg, chatbot]
|
| 361 |
)
|
| 362 |
|
| 363 |
if __name__ == "__main__":
|
| 364 |
+
demo.launch()
|