Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,89 +1,90 @@
|
|
1 |
-
import gradio as gr
|
2 |
-
from huggingface_hub import InferenceClient
|
3 |
-
|
4 |
-
import gradio as gr
|
5 |
-
from huggingface_hub import InferenceClient
|
6 |
import os
|
7 |
-
import zipfile
|
8 |
-
import
|
9 |
-
|
10 |
-
|
11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
zip_path = "solo_leveling_faiss_ko.zip"
|
13 |
extract_dir = "solo_leveling_faiss_ko"
|
14 |
-
|
15 |
if os.path.exists(zip_path) and not os.path.exists(extract_dir):
|
16 |
with zipfile.ZipFile(zip_path, 'r') as zip_ref:
|
17 |
zip_ref.extractall(extract_dir)
|
18 |
print(f"[INFO] ์์ถ ํด์ ์๋ฃ โ {extract_dir}")
|
19 |
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
""
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
response = ""
|
53 |
-
|
54 |
-
for message in client.chat_completion(
|
55 |
-
messages,
|
56 |
-
max_tokens=max_tokens,
|
57 |
-
stream=True,
|
58 |
-
temperature=temperature,
|
59 |
-
top_p=top_p,
|
60 |
-
):
|
61 |
-
token = message.choices[0].delta.content
|
62 |
-
|
63 |
-
response += token
|
64 |
-
yield response
|
65 |
-
|
66 |
-
|
67 |
-
"""
|
68 |
-
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
69 |
-
"""
|
70 |
-
demo = gr.ChatInterface(
|
71 |
-
respond,
|
72 |
-
additional_inputs=[
|
73 |
-
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
74 |
-
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
75 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
76 |
-
gr.Slider(
|
77 |
-
minimum=0.1,
|
78 |
-
maximum=1.0,
|
79 |
-
value=0.95,
|
80 |
-
step=0.05,
|
81 |
-
label="Top-p (nucleus sampling)",
|
82 |
-
),
|
83 |
-
],
|
84 |
)
|
85 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
86 |
|
87 |
if __name__ == "__main__":
|
88 |
-
import r_story_test
|
89 |
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import os
|
2 |
+
import zipfile
|
3 |
+
import pandas as pd
|
4 |
+
import gradio as gr
|
5 |
+
from langchain_community.vectorstores import FAISS
|
6 |
+
from langchain_community.embeddings import HuggingFaceEmbeddings
|
7 |
+
from langchain.chains import RetrievalQA
|
8 |
+
from langchain.prompts import PromptTemplate
|
9 |
+
from langchain_community.llms import HuggingFacePipeline
|
10 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
|
11 |
+
import torch
|
12 |
+
|
13 |
+
# ====== ZIP ์๋ ํด์ ======
|
14 |
zip_path = "solo_leveling_faiss_ko.zip"
|
15 |
extract_dir = "solo_leveling_faiss_ko"
|
|
|
16 |
if os.path.exists(zip_path) and not os.path.exists(extract_dir):
|
17 |
with zipfile.ZipFile(zip_path, 'r') as zip_ref:
|
18 |
zip_ref.extractall(extract_dir)
|
19 |
print(f"[INFO] ์์ถ ํด์ ์๋ฃ โ {extract_dir}")
|
20 |
|
21 |
+
# ====== ๋ฐ์ดํฐ ๋ก๋ ======
|
22 |
+
df = pd.read_csv("sl_webtoon_full_data_sequential.tsv", sep="\t")
|
23 |
+
df['row_id'] = df.index
|
24 |
+
df['text'] = df.apply(
|
25 |
+
lambda x: f"[{x['์ํผ์๋']}] #{x['row_id']} {x['type']} {x['scene_text']}",
|
26 |
+
axis=1
|
27 |
+
)
|
28 |
+
texts = df['text'].tolist()
|
29 |
+
|
30 |
+
# ====== FAISS ๋ก๋ ======
|
31 |
+
embedding_model = HuggingFaceEmbeddings(model_name='jhgan/ko-sroberta-multitask')
|
32 |
+
vectorstore = FAISS.load_local(extract_dir, embedding_model, allow_dangerous_deserialization=True)
|
33 |
+
|
34 |
+
# ====== ๋ชจ๋ธ ๋ก๋ (CPU ์ ์ฉ) ======
|
35 |
+
model_name = "kakaocorp/kanana-nano-2.1b-instruct"
|
36 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
37 |
+
model = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype=torch.float32).to("cpu")
|
38 |
+
llm_pipeline = pipeline("text-generation", model=model, tokenizer=tokenizer, max_new_tokens=200)
|
39 |
+
llm = HuggingFacePipeline(pipeline=llm_pipeline)
|
40 |
+
|
41 |
+
# ====== QA ์ฒด์ธ ======
|
42 |
+
custom_prompt = PromptTemplate(
|
43 |
+
input_variables=["context", "question"],
|
44 |
+
template="๋ค์ ๋ฌธ๋งฅ์ ์ฐธ๊ณ ํ์ฌ ์ง๋ฌธ์ ๋ตํ์ธ์.\n\n๋ฌธ๋งฅ:\n{context}\n\n์ง๋ฌธ:\n{question}\n\n๋ต๋ณ:"
|
45 |
+
)
|
46 |
+
qa_chain = RetrievalQA.from_chain_type(
|
47 |
+
llm=llm,
|
48 |
+
retriever=vectorstore.as_retriever(search_kwargs={"k": 5}),
|
49 |
+
chain_type="stuff",
|
50 |
+
return_source_documents=True,
|
51 |
+
chain_type_kwargs={"prompt": custom_prompt}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
52 |
)
|
53 |
|
54 |
+
# ====== ์ ํ์ง ======
|
55 |
+
choices = [
|
56 |
+
"ํฉ๋์ ๋ฌด๋ฆฌ๋ฅผ ๋ชจ๋ ์ฒ์นํ๋ค.",
|
57 |
+
"์งํธ๋ฅผ ํฌํจํ ํฉ๋์ ๋ฌด๋ฆฌ๋ฅผ ๋ชจ๋ ์ฒ์นํ๋ค.",
|
58 |
+
"์ ๋ถ ๊ธฐ์ ์ํค๊ณ ์ด๋ ค๋๋ค.",
|
59 |
+
"์์คํ
์ ๊ฑฐ๋ถํ๊ณ ๊ทธ๋ฅ ๋๋ง์น๋ค."
|
60 |
+
]
|
61 |
+
|
62 |
+
# ====== Gradio ํจ์ ======
|
63 |
+
def run_episode(selection):
|
64 |
+
user_choice = choices[int(selection) - 1]
|
65 |
+
result = qa_chain({"query": user_choice})
|
66 |
+
retrieved_context = "\n".join([doc.page_content for doc in result["source_documents"]])
|
67 |
+
|
68 |
+
prompt = f"""
|
69 |
+
๋น์ ์ ์นํฐ '๋ ํผ์๋ง ๋ ๋ฒจ์
'์ ์ฑ์ง์ฐ์
๋๋ค.
|
70 |
+
ํ์ฌ ์ํฉ:
|
71 |
+
{retrieved_context}
|
72 |
+
์ฌ์ฉ์ ์ ํ: {user_choice}
|
73 |
+
์ฑ์ง์ฐ์ ๋งํฌ๋ก ๊ฐ๊ฒฐํ๊ณ ์์ฐ์ค๋ฌ์ด ๏ฟฝ๏ฟฝ๏ฟฝ์ฌ๋ฅผ 1~2๋ฌธ์ฅ ์์ฑํ์ธ์.
|
74 |
+
์ค๋ณต๋ ๋ด์ฉ์ด๋ ๋น์ทํ ๋ฌธ์ฅ์ ๋ง๋ค์ง ๋ง์ธ์.
|
75 |
+
"""
|
76 |
+
|
77 |
+
response = llm_pipeline(prompt)[0]["generated_text"]
|
78 |
+
return f"[์ฑ์ง์ฐ ์๋ต]\n{response}"
|
79 |
+
|
80 |
+
# ====== Gradio UI ======
|
81 |
+
demo = gr.Interface(
|
82 |
+
fn=run_episode,
|
83 |
+
inputs=gr.Dropdown(choices=["1", "2", "3", "4"], label="์ ํ ๋ฒํธ", type="value"),
|
84 |
+
outputs="text",
|
85 |
+
title="์ฑ์ง์ฐ ์ ํ ์๋ฎฌ๋ ์ด์
",
|
86 |
+
description="๋ฒํธ๋ฅผ ์ ํํ๋ฉด ์ฑ์ง์ฐ์ ์๋ต์ด ์์ฑ๋ฉ๋๋ค."
|
87 |
+
)
|
88 |
|
89 |
if __name__ == "__main__":
|
|
|
90 |
demo.launch()
|