Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,21 +1,32 @@
|
|
1 |
import gradio as gr
|
2 |
import torch
|
3 |
-
from transformers import AutoModelForCausalLM, AutoTokenizer
|
4 |
from PyPDF2 import PdfReader
|
5 |
import google.generativeai as genai
|
6 |
import os
|
7 |
from langsmith import Client
|
8 |
from ragas.metrics import faithfulness, answer_relevancy, context_relevancy
|
9 |
|
|
|
|
|
|
|
10 |
# 加載模型
|
11 |
-
openelm_model = AutoModelForCausalLM.from_pretrained(
|
12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
|
14 |
-
# Gemini API
|
15 |
GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY")
|
16 |
genai.configure(api_key=GOOGLE_API_KEY)
|
17 |
|
18 |
-
# LangSmith
|
19 |
os.environ["LANGCHAIN_API_KEY"] = "your_langchain_api_key"
|
20 |
os.environ["LANGCHAIN_TRACING_V2"] = "true"
|
21 |
os.environ["LANGCHAIN_ENDPOINT"] = "https://api.smith.langchain.com"
|
@@ -35,11 +46,11 @@ def gemini_generate(prompt, max_tokens):
|
|
35 |
return response.text
|
36 |
|
37 |
def openelm_generate(prompt, max_tokens):
|
38 |
-
|
39 |
output_ids = openelm_model.generate(
|
40 |
-
input_ids,
|
41 |
max_length=max_tokens,
|
42 |
-
pad_token_id=
|
43 |
)
|
44 |
return openelm_tokenizer.decode(output_ids[0], skip_special_tokens=True)
|
45 |
|
@@ -56,21 +67,42 @@ def process_query(pdf_file, llm_choice, query, max_tokens, api_key):
|
|
56 |
GOOGLE_API_KEY = api_key
|
57 |
genai.configure(api_key=GOOGLE_API_KEY)
|
58 |
|
|
|
59 |
pdf_path = pdf_file.name
|
60 |
context = extract_text_from_pdf(pdf_path)
|
61 |
|
|
|
62 |
if llm_choice == "Gemini":
|
63 |
response = gemini_generate(f"上下文: {context}\n問題: {query}", max_tokens)
|
64 |
else: # OpenELM
|
65 |
response = openelm_generate(f"上下文: {context}\n問題: {query}", max_tokens)
|
66 |
|
|
|
67 |
faith_score, ans_rel_score, ctx_rel_score = evaluate_response(response, context, query)
|
68 |
|
69 |
return response, faith_score, ans_rel_score, ctx_rel_score
|
70 |
except Exception as e:
|
71 |
-
return str(e), 0, 0, 0
|
72 |
|
73 |
-
# Gradio
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
74 |
|
75 |
if __name__ == "__main__":
|
76 |
-
iface.launch()
|
|
|
1 |
import gradio as gr
|
2 |
import torch
|
3 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
4 |
from PyPDF2 import PdfReader
|
5 |
import google.generativeai as genai
|
6 |
import os
|
7 |
from langsmith import Client
|
8 |
from ragas.metrics import faithfulness, answer_relevancy, context_relevancy
|
9 |
|
10 |
+
# 更新的 langchain_community 導入
|
11 |
+
from langchain_community.llms import OpenAI # 示例導入
|
12 |
+
|
13 |
# 加載模型
|
14 |
+
openelm_model = AutoModelForCausalLM.from_pretrained(
|
15 |
+
"apple/OpenELM-270M",
|
16 |
+
trust_remote_code=True
|
17 |
+
)
|
18 |
+
|
19 |
+
# 加載 tokenizer,確保 trust_remote_code=True
|
20 |
+
openelm_tokenizer = AutoTokenizer.from_pretrained(
|
21 |
+
"apple/OpenELM-270M",
|
22 |
+
trust_remote_code=True
|
23 |
+
)
|
24 |
|
25 |
+
# 設置 Gemini API
|
26 |
GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY")
|
27 |
genai.configure(api_key=GOOGLE_API_KEY)
|
28 |
|
29 |
+
# 設置 LangSmith
|
30 |
os.environ["LANGCHAIN_API_KEY"] = "your_langchain_api_key"
|
31 |
os.environ["LANGCHAIN_TRACING_V2"] = "true"
|
32 |
os.environ["LANGCHAIN_ENDPOINT"] = "https://api.smith.langchain.com"
|
|
|
46 |
return response.text
|
47 |
|
48 |
def openelm_generate(prompt, max_tokens):
|
49 |
+
tokenized_prompt = openelm_tokenizer(prompt, return_tensors="pt")
|
50 |
output_ids = openelm_model.generate(
|
51 |
+
tokenized_prompt["input_ids"],
|
52 |
max_length=max_tokens,
|
53 |
+
pad_token_id=0,
|
54 |
)
|
55 |
return openelm_tokenizer.decode(output_ids[0], skip_special_tokens=True)
|
56 |
|
|
|
67 |
GOOGLE_API_KEY = api_key
|
68 |
genai.configure(api_key=GOOGLE_API_KEY)
|
69 |
|
70 |
+
# 從 PDF 提取文本
|
71 |
pdf_path = pdf_file.name
|
72 |
context = extract_text_from_pdf(pdf_path)
|
73 |
|
74 |
+
# 根據選擇的 LLM 生成回應
|
75 |
if llm_choice == "Gemini":
|
76 |
response = gemini_generate(f"上下文: {context}\n問題: {query}", max_tokens)
|
77 |
else: # OpenELM
|
78 |
response = openelm_generate(f"上下文: {context}\n問題: {query}", max_tokens)
|
79 |
|
80 |
+
# 評估回應
|
81 |
faith_score, ans_rel_score, ctx_rel_score = evaluate_response(response, context, query)
|
82 |
|
83 |
return response, faith_score, ans_rel_score, ctx_rel_score
|
84 |
except Exception as e:
|
85 |
+
return str(e), 0, 0, 0 # 返回錯誤消息和零分數
|
86 |
|
87 |
+
# Gradio 介面
|
88 |
+
iface = gr.Interface(
|
89 |
+
fn=process_query,
|
90 |
+
inputs=[
|
91 |
+
gr.File(label="上傳 PDF"),
|
92 |
+
gr.Dropdown(["Gemini", "OpenELM"], label="選擇 LLM"),
|
93 |
+
gr.Textbox(label="輸入您的問題"),
|
94 |
+
gr.Slider(minimum=50, maximum=1000, step=50, label="最大令牌數"),
|
95 |
+
gr.Textbox(label="Gemini API 金鑰 (可選)", type="password")
|
96 |
+
],
|
97 |
+
outputs=[
|
98 |
+
gr.Textbox(label="生成的答案"),
|
99 |
+
gr.Number(label="真實性得分"),
|
100 |
+
gr.Number(label="答案相關性得分"),
|
101 |
+
gr.Number(label="上下文相關性得分")
|
102 |
+
],
|
103 |
+
title="多模型 LLM 查詢介面,支持 PDF 上下文",
|
104 |
+
description="上傳 PDF,選擇 LLM,並提出問題。回應將使用 RAGAS 指標進行評估。"
|
105 |
+
)
|
106 |
|
107 |
if __name__ == "__main__":
|
108 |
+
iface.launch()
|