min24ss commited on
Commit
a7ada72
ยท
verified ยท
1 Parent(s): 56413f5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +77 -76
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 nbformat
9
- from nbconvert.preprocessors import ExecutePreprocessor
10
-
11
- # zip ํŒŒ์ผ ์ž๋™ ํ•ด์ œ ์ฝ”๋“œ
 
 
 
 
 
 
 
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
- For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
24
- """
25
- client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
26
-
27
- def run_notebook(path):
28
- with open(path, encoding='utf-8') as f:
29
- nb = nbformat.read(f, as_version=4)
30
- ep = ExecutePreprocessor(timeout=600, kernel_name='python3')
31
- ep.preprocess(nb, {'metadata': {'path': './'}}) # ํ˜„์žฌ ๊ฒฝ๋กœ์—์„œ ์‹คํ–‰
32
- print(f"[INFO] Notebook {path} ์‹คํ–‰ ์™„๋ฃŒ")
33
-
34
- def respond(
35
- message,
36
- history: list[tuple[str, str]],
37
- system_message,
38
- max_tokens,
39
- temperature,
40
- top_p,
41
- ):
42
- messages = [{"role": "system", "content": system_message}]
43
-
44
- for val in history:
45
- if val[0]:
46
- messages.append({"role": "user", "content": val[0]})
47
- if val[1]:
48
- messages.append({"role": "assistant", "content": val[1]})
49
-
50
- messages.append({"role": "user", "content": message})
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()