Spaces:
Sleeping
Sleeping
Commit
·
4876642
1
Parent(s):
41bd965
sarashinaをr2に変更
Browse files- .gitignore +0 -0
- .idea/.gitignore +10 -0
- .idea/inspectionProfiles/profiles_settings.xml +6 -0
- .idea/misc.xml +7 -0
- .idea/modules.xml +8 -0
- .idea/processing_test.iml +8 -0
- .idea/vcs.xml +6 -0
- app.py +8 -12
- requirements.txt +3 -3
.gitignore
DELETED
|
File without changes
|
.idea/.gitignore
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# デフォルトの無視対象ファイル
|
| 2 |
+
/shelf/
|
| 3 |
+
/workspace.xml
|
| 4 |
+
# エディターベースの HTTP クライアントリクエスト
|
| 5 |
+
/httpRequests/
|
| 6 |
+
# Datasource local storage ignored files
|
| 7 |
+
/dataSources/
|
| 8 |
+
/dataSources.local.xml
|
| 9 |
+
|
| 10 |
+
.idea/
|
.idea/inspectionProfiles/profiles_settings.xml
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<component name="InspectionProjectProfileManager">
|
| 2 |
+
<settings>
|
| 3 |
+
<option name="USE_PROJECT_PROFILE" value="false" />
|
| 4 |
+
<version value="1.0" />
|
| 5 |
+
</settings>
|
| 6 |
+
</component>
|
.idea/misc.xml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<?xml version="1.0" encoding="UTF-8"?>
|
| 2 |
+
<project version="4">
|
| 3 |
+
<component name="Black">
|
| 4 |
+
<option name="sdkName" value="Python 3.9 (Clang-to-japanese)" />
|
| 5 |
+
</component>
|
| 6 |
+
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.9 (Clang-to-japanese)" project-jdk-type="Python SDK" />
|
| 7 |
+
</project>
|
.idea/modules.xml
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<?xml version="1.0" encoding="UTF-8"?>
|
| 2 |
+
<project version="4">
|
| 3 |
+
<component name="ProjectModuleManager">
|
| 4 |
+
<modules>
|
| 5 |
+
<module fileurl="file://$PROJECT_DIR$/.idea/processing_test.iml" filepath="$PROJECT_DIR$/.idea/processing_test.iml" />
|
| 6 |
+
</modules>
|
| 7 |
+
</component>
|
| 8 |
+
</project>
|
.idea/processing_test.iml
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<?xml version="1.0" encoding="UTF-8"?>
|
| 2 |
+
<module type="PYTHON_MODULE" version="4">
|
| 3 |
+
<component name="NewModuleRootManager">
|
| 4 |
+
<content url="file://$MODULE_DIR$" />
|
| 5 |
+
<orderEntry type="inheritedJdk" />
|
| 6 |
+
<orderEntry type="sourceFolder" forTests="false" />
|
| 7 |
+
</component>
|
| 8 |
+
</module>
|
.idea/vcs.xml
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<?xml version="1.0" encoding="UTF-8"?>
|
| 2 |
+
<project version="4">
|
| 3 |
+
<component name="VcsDirectoryMappings">
|
| 4 |
+
<mapping directory="" vcs="Git" />
|
| 5 |
+
</component>
|
| 6 |
+
</project>
|
app.py
CHANGED
|
@@ -1,15 +1,14 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
"""
|
| 3 |
import pathlib
|
| 4 |
from huggingface_hub import hf_hub_download
|
| 5 |
from llama_cpp import Llama
|
| 6 |
-
"""
|
| 7 |
from sentence_transformers import SentenceTransformer
|
| 8 |
import faiss
|
| 9 |
import numpy as np
|
| 10 |
import pandas as pd
|
| 11 |
|
| 12 |
## LLMの読み込み(Qwen2.5-3Bをsafetensorsで読み込み)
|
|
|
|
| 13 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 14 |
import torch
|
| 15 |
|
|
@@ -17,20 +16,19 @@ model_name = "Qwen/Qwen2.5-3B"
|
|
| 17 |
tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
|
| 18 |
llm = AutoModelForCausalLM.from_pretrained(model_name, trust_remote_code=True, torch_dtype=torch.float16)
|
| 19 |
llm.eval()
|
| 20 |
-
|
| 21 |
"""
|
|
|
|
| 22 |
## LLMの読み込み
|
| 23 |
models_dir = pathlib.Path(__file__).parent / "models"
|
| 24 |
models_dir.mkdir(exist_ok=True)
|
| 25 |
|
| 26 |
model_path = hf_hub_download(
|
| 27 |
-
repo_id="Mori-kamiyama/sarashina2-13b-
|
| 28 |
filename="model.gguf",
|
| 29 |
local_dir=models_dir
|
| 30 |
)
|
| 31 |
|
| 32 |
llm = Llama(model_path=model_path)
|
| 33 |
-
"""
|
| 34 |
|
| 35 |
## 埋め込みモデルの読み込み
|
| 36 |
model = SentenceTransformer("BAAI/bge-small-en-v1.5")
|
|
@@ -50,12 +48,8 @@ index.add(np.array(doc_embeddings))
|
|
| 50 |
|
| 51 |
def generate_text(prompt):
|
| 52 |
full_prompt = search(prompt)
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
with torch.no_grad():
|
| 56 |
-
output_ids = llm.generate(**inputs, max_new_tokens=256)
|
| 57 |
-
result_text = tokenizer.decode(output_ids[0], skip_special_tokens=True)
|
| 58 |
-
return result_text
|
| 59 |
|
| 60 |
def search(query):
|
| 61 |
query_embedding = model.encode([query], normalize_embeddings=True)
|
|
@@ -72,7 +66,9 @@ def search(query):
|
|
| 72 |
print(f"→ {doc_text}")
|
| 73 |
|
| 74 |
# RAG用のプロンプトを作成
|
| 75 |
-
prompt = "
|
|
|
|
|
|
|
| 76 |
prompt += "\n".join(retrieved_docs)
|
| 77 |
prompt += f"\n\n質問: {query}"
|
| 78 |
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
import pathlib
|
| 3 |
from huggingface_hub import hf_hub_download
|
| 4 |
from llama_cpp import Llama
|
|
|
|
| 5 |
from sentence_transformers import SentenceTransformer
|
| 6 |
import faiss
|
| 7 |
import numpy as np
|
| 8 |
import pandas as pd
|
| 9 |
|
| 10 |
## LLMの読み込み(Qwen2.5-3Bをsafetensorsで読み込み)
|
| 11 |
+
"""
|
| 12 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 13 |
import torch
|
| 14 |
|
|
|
|
| 16 |
tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
|
| 17 |
llm = AutoModelForCausalLM.from_pretrained(model_name, trust_remote_code=True, torch_dtype=torch.float16)
|
| 18 |
llm.eval()
|
|
|
|
| 19 |
"""
|
| 20 |
+
|
| 21 |
## LLMの読み込み
|
| 22 |
models_dir = pathlib.Path(__file__).parent / "models"
|
| 23 |
models_dir.mkdir(exist_ok=True)
|
| 24 |
|
| 25 |
model_path = hf_hub_download(
|
| 26 |
+
repo_id="Mori-kamiyama/sarashina2-13b-r2",
|
| 27 |
filename="model.gguf",
|
| 28 |
local_dir=models_dir
|
| 29 |
)
|
| 30 |
|
| 31 |
llm = Llama(model_path=model_path)
|
|
|
|
| 32 |
|
| 33 |
## 埋め込みモデルの読み込み
|
| 34 |
model = SentenceTransformer("BAAI/bge-small-en-v1.5")
|
|
|
|
| 48 |
|
| 49 |
def generate_text(prompt):
|
| 50 |
full_prompt = search(prompt)
|
| 51 |
+
output = llm(full_prompt, max_tokens=256)
|
| 52 |
+
return output["choices"][0]["text"]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
def search(query):
|
| 55 |
query_embedding = model.encode([query], normalize_embeddings=True)
|
|
|
|
| 66 |
print(f"→ {doc_text}")
|
| 67 |
|
| 68 |
# RAG用のプロンプトを作成
|
| 69 |
+
prompt = "以下の文書を参照して質問に答えてください。"
|
| 70 |
+
prompt += "生成は以下のフォーマットで回答しなさい\n<reasoning>\n...\n</reasoning>\n\n<answer>\n...\n</answer>"
|
| 71 |
+
prompt += "\n\n文書:\n"
|
| 72 |
prompt += "\n".join(retrieved_docs)
|
| 73 |
prompt += f"\n\n質問: {query}"
|
| 74 |
|
requirements.txt
CHANGED
|
@@ -1,7 +1,7 @@
|
|
| 1 |
gradio
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
sentence-transformers
|
| 6 |
faiss-cpu
|
| 7 |
numpy
|
|
|
|
| 1 |
gradio
|
| 2 |
+
llama-cpp-python
|
| 3 |
+
huggingface_hub
|
| 4 |
+
pathlib
|
| 5 |
sentence-transformers
|
| 6 |
faiss-cpu
|
| 7 |
numpy
|