Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,48 +1,56 @@
|
|
1 |
import gradio as gr
|
2 |
from huggingface_hub import InferenceClient
|
3 |
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
|
|
|
|
|
|
|
|
8 |
|
|
|
|
|
9 |
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
top_p,
|
17 |
-
):
|
18 |
-
messages = [{"role": "system", "content": system_message}]
|
19 |
|
20 |
-
|
21 |
-
|
22 |
-
messages.append({"role": "user", "content": val[0]})
|
23 |
-
if val[1]:
|
24 |
-
messages.append({"role": "assistant", "content": val[1]})
|
25 |
|
26 |
-
|
|
|
|
|
|
|
27 |
|
28 |
-
|
|
|
|
|
|
|
29 |
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
|
|
|
|
38 |
|
39 |
-
|
40 |
-
|
|
|
|
|
|
|
|
|
41 |
|
42 |
-
|
43 |
-
"""
|
44 |
-
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
45 |
-
"""
|
46 |
demo = gr.ChatInterface(
|
47 |
respond,
|
48 |
additional_inputs=[
|
|
|
1 |
import gradio as gr
|
2 |
from huggingface_hub import InferenceClient
|
3 |
|
4 |
+
from fastapi import FastAPI, HTTPException, Request
|
5 |
+
from pydantic import BaseModel
|
6 |
+
from typing import List
|
7 |
+
from sentence_transformers import CrossEncoder, util
|
8 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
9 |
+
import torch
|
10 |
+
import numpy as np
|
11 |
+
from qa_vector_store import build_qa_vector_store, retrieve_and_rerank, generate_response_from_local_llm
|
12 |
|
13 |
+
# 建立 FastAPI 應用
|
14 |
+
app = FastAPI()
|
15 |
|
16 |
+
# 初始化模型和資料庫
|
17 |
+
model_name = "sentence-transformers/paraphrase-multilingual-MiniLM-L12-v2"
|
18 |
+
collection_name = model_name.split("/")[-1]
|
19 |
+
cross_encoder_model = "cross-encoder/mmarco-mMiniLMv2-L12-H384-v1"
|
20 |
+
tokenizer = AutoTokenizer.from_pretrained("Qwen/Qwen2-0.5B-Instruct", trust_remote_code=True)
|
21 |
+
llm_model = AutoModelForCausalLM.from_pretrained("Qwen/Qwen2-0.5B-Instruct", trust_remote_code=True)
|
|
|
|
|
|
|
22 |
|
23 |
+
# 構建向量資料庫
|
24 |
+
build_qa_vector_store(model_name, collection_name)
|
|
|
|
|
|
|
25 |
|
26 |
+
# 輸入格式
|
27 |
+
class QueryInput(BaseModel):
|
28 |
+
query: str
|
29 |
+
top_k: int = 5
|
30 |
|
31 |
+
# 輸出格式
|
32 |
+
class SearchResult(BaseModel):
|
33 |
+
text: str
|
34 |
+
score: float
|
35 |
|
36 |
+
# 搜尋+rerank API
|
37 |
+
@app.post("/search", response_model=List[SearchResult])
|
38 |
+
def search(input: QueryInput):
|
39 |
+
reranked = retrieve_and_rerank(input.query, model_name, collection_name, cross_encoder_model, score_threshold=0.5, search_top_k=20, rerank_top_k=input.top_k)
|
40 |
+
|
41 |
+
# 如果沒有找到相關答案,則返回 404 錯誤
|
42 |
+
if not reranked:
|
43 |
+
raise HTTPException(status_code=404, detail="找不到相關答案,請嘗試換個問題或降低門檻。")
|
44 |
+
|
45 |
+
final_passages = [r[0] for r in reranked]
|
46 |
|
47 |
+
# 使用 LLM 生成回答
|
48 |
+
answer = generate_response_from_local_llm(input.query, final_passages, tokenizer, llm_model, max_new_tokens=256)
|
49 |
+
|
50 |
+
if not answer:
|
51 |
+
raise HTTPException(status_code=404, detail="無法生成回答,請檢查輸入或模型設定。")
|
52 |
+
return answer
|
53 |
|
|
|
|
|
|
|
|
|
54 |
demo = gr.ChatInterface(
|
55 |
respond,
|
56 |
additional_inputs=[
|