Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,68 +1,102 @@
|
|
1 |
import gradio as gr
|
2 |
-
from gradio import Interface, Textbox, Image, Markdown
|
3 |
-
from transformers import AutoModelForCausalLM, AutoTokenizer, CLIPProcessor, CLIPModel
|
4 |
import torch
|
5 |
-
from
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
|
7 |
-
#
|
8 |
-
|
9 |
-
|
10 |
-
clip_model = CLIPModel.from_pretrained("openai/clip-vit-base-patch32")
|
11 |
-
clip_processor = CLIPProcessor.from_pretrained("openai/clip-vit-base-patch32")
|
12 |
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
tokenized_prompt["input_ids"],
|
18 |
-
max_length=
|
19 |
pad_token_id=0,
|
20 |
)
|
21 |
-
|
22 |
-
|
23 |
-
|
|
|
|
|
|
|
|
|
|
|
24 |
|
25 |
-
def
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
return image_features
|
31 |
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
|
36 |
-
#
|
37 |
-
|
|
|
|
|
|
|
|
|
|
|
38 |
|
39 |
-
#
|
40 |
-
|
41 |
|
42 |
-
return
|
43 |
-
|
44 |
-
def greet(text_input, image_input, num_tokens):
|
45 |
-
if image_input is not None:
|
46 |
-
return generate_multimodal(text_input, image_input, num_tokens)
|
47 |
-
else:
|
48 |
-
return generate_text(text_input, num_tokens)
|
49 |
-
|
50 |
-
developer_info = """
|
51 |
-
This space is developed by Ahmadreza Anaami
|
52 |
-
Feel free to set via API key too
|
53 |
-
Models used: apple/OpenELM-270M, openai/clip-vit-base-patch32
|
54 |
-
"""
|
55 |
|
|
|
56 |
iface = gr.Interface(
|
57 |
-
fn=
|
58 |
inputs=[
|
59 |
-
|
60 |
-
|
61 |
-
Textbox(label="
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
62 |
],
|
63 |
-
|
64 |
-
|
65 |
-
description=developer_info,
|
66 |
css="""
|
67 |
#dev-info {
|
68 |
font-size: 0.8rem;
|
|
|
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 (
|
9 |
+
faithfulness, answer_relevancy, context_relevancy
|
10 |
+
)
|
11 |
|
12 |
+
# 加載模型
|
13 |
+
openelm_model = AutoModelForCausalLM.from_pretrained("apple/OpenELM-270M", trust_remote_code=True)
|
14 |
+
openelm_tokenizer = AutoTokenizer.from_pretrained("NousResearch/Llama-2-7b-hf")
|
|
|
|
|
15 |
|
16 |
+
# Gemini API 設置
|
17 |
+
GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY")
|
18 |
+
genai.configure(api_key=GOOGLE_API_KEY)
|
19 |
+
|
20 |
+
# LangSmith 設置
|
21 |
+
os.environ["LANGCHAIN_API_KEY"] = "your_langchain_api_key"
|
22 |
+
os.environ["LANGCHAIN_TRACING_V2"] = "true"
|
23 |
+
os.environ["LANGCHAIN_ENDPOINT"] = "https://api.smith.langchain.com"
|
24 |
+
client = Client()
|
25 |
+
|
26 |
+
def extract_text_from_pdf(pdf_path):
|
27 |
+
with open(pdf_path, 'rb') as file:
|
28 |
+
reader = PdfReader(file)
|
29 |
+
text = ""
|
30 |
+
for page in reader.pages:
|
31 |
+
text += page.extract_text() + "\n"
|
32 |
+
return text
|
33 |
+
|
34 |
+
def gemini_generate(prompt, max_tokens):
|
35 |
+
model = genai.GenerativeModel('gemini-pro')
|
36 |
+
response = model.generate_content(prompt, max_output_tokens=max_tokens)
|
37 |
+
return response.text
|
38 |
+
|
39 |
+
def nvidia_generate(prompt, max_tokens):
|
40 |
+
# 這裡需要實現 Nvidia API 調用
|
41 |
+
return "Nvidia API 尚未實現"
|
42 |
+
|
43 |
+
def openelm_generate(prompt, max_tokens):
|
44 |
+
tokenized_prompt = openelm_tokenizer(prompt, return_tensors="pt")
|
45 |
+
output_ids = openelm_model.generate(
|
46 |
tokenized_prompt["input_ids"],
|
47 |
+
max_length=max_tokens,
|
48 |
pad_token_id=0,
|
49 |
)
|
50 |
+
return openelm_tokenizer.decode(output_ids[0], skip_special_tokens=True)
|
51 |
+
|
52 |
+
def evaluate_response(response, context, query):
|
53 |
+
# 使用 RAGAS 評估回答
|
54 |
+
faith_score = faithfulness.score([response], [context], [query])
|
55 |
+
ans_rel_score = answer_relevancy.score([response], [query])
|
56 |
+
ctx_rel_score = context_relevancy.score([response], [context], [query])
|
57 |
+
return faith_score, ans_rel_score, ctx_rel_score
|
58 |
|
59 |
+
def process_query(pdf_file, llm_choice, query, max_tokens, api_key):
|
60 |
+
global GOOGLE_API_KEY
|
61 |
+
if api_key:
|
62 |
+
GOOGLE_API_KEY = api_key
|
63 |
+
genai.configure(api_key=GOOGLE_API_KEY)
|
|
|
64 |
|
65 |
+
# 從 PDF 提取文本
|
66 |
+
pdf_path = pdf_file.name
|
67 |
+
context = extract_text_from_pdf(pdf_path)
|
68 |
|
69 |
+
# 根據選擇的 LLM 生成回應
|
70 |
+
if llm_choice == "Gemini":
|
71 |
+
response = gemini_generate(f"上下文: {context}\n問題: {query}", max_tokens)
|
72 |
+
elif llm_choice == "Nvidia":
|
73 |
+
response = nvidia_generate(f"上下文: {context}\n問題: {query}", max_tokens)
|
74 |
+
else: # OpenELM
|
75 |
+
response = openelm_generate(f"上下文: {context}\n問題: {query}", max_tokens)
|
76 |
|
77 |
+
# 評估回應
|
78 |
+
faith_score, ans_rel_score, ctx_rel_score = evaluate_response(response, context, query)
|
79 |
|
80 |
+
return response, faith_score, ans_rel_score, ctx_rel_score
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
81 |
|
82 |
+
# Gradio 介面
|
83 |
iface = gr.Interface(
|
84 |
+
fn=process_query,
|
85 |
inputs=[
|
86 |
+
gr.File(label="上傳 PDF"),
|
87 |
+
gr.Dropdown(["Gemini", "Nvidia", "OpenELM"], label="選擇 LLM"),
|
88 |
+
gr.Textbox(label="輸入您的問題"),
|
89 |
+
gr.Slider(minimum=50, maximum=1000, step=50, label="最大令牌數"),
|
90 |
+
gr.Textbox(label="Gemini API Key (可選)", type="password")
|
91 |
+
],
|
92 |
+
outputs=[
|
93 |
+
gr.Textbox(label="生成的答案"),
|
94 |
+
gr.Number(label="真實性得分"),
|
95 |
+
gr.Number(label="答案相關性得分"),
|
96 |
+
gr.Number(label="上下文相關性得分")
|
97 |
],
|
98 |
+
title="多模型 LLM 查詢介面,支持 PDF 上下文",
|
99 |
+
description="上傳 PDF,選擇 LLM,並提出問題。回應將使用 RAGAS 指標進行評估。",
|
|
|
100 |
css="""
|
101 |
#dev-info {
|
102 |
font-size: 0.8rem;
|