Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,16 +1,44 @@
|
|
1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
2 |
from huggingface_hub import InferenceClient
|
3 |
from typing import List, Tuple
|
4 |
|
5 |
# Default settings
|
6 |
class ChatConfig:
|
7 |
MODEL = "google/gemma-3-27b-it"
|
8 |
-
DEFAULT_SYSTEM_MSG = "You are
|
9 |
DEFAULT_MAX_TOKENS = 512
|
10 |
DEFAULT_TEMP = 0.3
|
11 |
DEFAULT_TOP_P = 0.95
|
12 |
|
13 |
client = InferenceClient(ChatConfig.MODEL)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
|
15 |
def generate_response(
|
16 |
message: str,
|
@@ -20,17 +48,20 @@ def generate_response(
|
|
20 |
temperature: float = ChatConfig.DEFAULT_TEMP,
|
21 |
top_p: float = ChatConfig.DEFAULT_TOP_P
|
22 |
) -> str:
|
|
|
|
|
|
|
|
|
|
|
23 |
messages = [{"role": "system", "content": system_message}]
|
24 |
-
|
25 |
-
# Conversation history
|
26 |
for user_msg, bot_msg in history:
|
27 |
if user_msg:
|
28 |
messages.append({"role": "user", "content": user_msg})
|
29 |
if bot_msg:
|
30 |
messages.append({"role": "assistant", "content": bot_msg})
|
31 |
-
|
32 |
-
messages.append({"role": "user", "content": message})
|
33 |
-
|
34 |
response = ""
|
35 |
for chunk in client.chat_completion(
|
36 |
messages,
|
@@ -43,90 +74,39 @@ def generate_response(
|
|
43 |
response += token
|
44 |
yield response
|
45 |
|
|
|
|
|
|
|
|
|
|
|
46 |
|
47 |
-
def create_interface() -> gr.
|
48 |
-
"""
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
}
|
62 |
-
.gr-button {
|
63 |
-
border-radius: 8px;
|
64 |
-
padding: 8px 16px;
|
65 |
-
}
|
66 |
-
"""
|
67 |
-
|
68 |
-
# Custom chatbot
|
69 |
-
chatbot = gr.Chatbot(
|
70 |
-
label="Gemma Chat",
|
71 |
-
avatar_images=("./user.png", "./botge.png"),
|
72 |
-
height=450,
|
73 |
-
show_copy_button=True
|
74 |
-
)
|
75 |
-
|
76 |
-
# Chat interface
|
77 |
-
interface = gr.ChatInterface(
|
78 |
-
fn=generate_response,
|
79 |
-
chatbot=chatbot,
|
80 |
-
title="欢迎体验 喵哥 Google-Gemma-3大模型",
|
81 |
-
theme=gr.themes.Soft(),
|
82 |
-
css=custom_css,
|
83 |
-
additional_inputs=[
|
84 |
-
gr.Textbox(
|
85 |
-
value=ChatConfig.DEFAULT_SYSTEM_MSG,
|
86 |
-
label="系统提示词",
|
87 |
-
lines=2,
|
88 |
-
placeholder="Enter system message..."
|
89 |
-
),
|
90 |
-
gr.Slider(
|
91 |
-
minimum=1,
|
92 |
-
maximum=8192,
|
93 |
-
value=ChatConfig.DEFAULT_MAX_TOKENS,
|
94 |
-
step=1,
|
95 |
-
label="Max Tokens",
|
96 |
-
info="Controls response length"
|
97 |
-
),
|
98 |
-
gr.Slider(
|
99 |
-
minimum=0.1,
|
100 |
-
maximum=1.0,
|
101 |
-
value=ChatConfig.DEFAULT_TEMP,
|
102 |
-
step=0.1,
|
103 |
-
label="Temperature",
|
104 |
-
info="Controls randomness"
|
105 |
-
),
|
106 |
-
gr.Slider(
|
107 |
-
minimum=0.1,
|
108 |
-
maximum=1.0,
|
109 |
-
value=ChatConfig.DEFAULT_TOP_P,
|
110 |
-
step=0.05,
|
111 |
-
label="Top-P",
|
112 |
-
info="Controls diversity"
|
113 |
-
)
|
114 |
-
],
|
115 |
-
additional_inputs_accordion=gr.Accordion(label="高级设置", open=False)
|
116 |
-
)
|
117 |
-
|
118 |
-
return interface
|
119 |
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
|
|
130 |
|
131 |
if __name__ == "__main__":
|
132 |
-
|
|
|
|
1 |
import gradio as gr
|
2 |
+
import fitz # PyMuPDF for PDF text extraction
|
3 |
+
import faiss # FAISS for vector search
|
4 |
+
import numpy as np
|
5 |
+
from sentence_transformers import SentenceTransformer
|
6 |
from huggingface_hub import InferenceClient
|
7 |
from typing import List, Tuple
|
8 |
|
9 |
# Default settings
|
10 |
class ChatConfig:
|
11 |
MODEL = "google/gemma-3-27b-it"
|
12 |
+
DEFAULT_SYSTEM_MSG = "You are an AI assistant answering only based on the uploaded PDF."
|
13 |
DEFAULT_MAX_TOKENS = 512
|
14 |
DEFAULT_TEMP = 0.3
|
15 |
DEFAULT_TOP_P = 0.95
|
16 |
|
17 |
client = InferenceClient(ChatConfig.MODEL)
|
18 |
+
embed_model = SentenceTransformer("all-MiniLM-L6-v2") # Lightweight embedding model
|
19 |
+
vector_dim = 384 # Embedding size
|
20 |
+
index = faiss.IndexFlatL2(vector_dim) # FAISS index
|
21 |
+
|
22 |
+
documents = [] # Store extracted text
|
23 |
+
|
24 |
+
def extract_text_from_pdf(pdf_path):
|
25 |
+
"""Extracts text from PDF"""
|
26 |
+
doc = fitz.open(pdf_path)
|
27 |
+
text_chunks = [page.get_text("text") for page in doc]
|
28 |
+
return text_chunks
|
29 |
+
|
30 |
+
def create_vector_db(text_chunks):
|
31 |
+
"""Embeds text chunks and adds them to FAISS index"""
|
32 |
+
global documents, index
|
33 |
+
documents = text_chunks
|
34 |
+
embeddings = embed_model.encode(text_chunks)
|
35 |
+
index.add(np.array(embeddings, dtype=np.float32))
|
36 |
+
|
37 |
+
def search_relevant_text(query):
|
38 |
+
"""Finds the most relevant text chunk for the given query"""
|
39 |
+
query_embedding = embed_model.encode([query])
|
40 |
+
_, closest_idx = index.search(np.array(query_embedding, dtype=np.float32), k=3)
|
41 |
+
return "\n".join([documents[i] for i in closest_idx[0]])
|
42 |
|
43 |
def generate_response(
|
44 |
message: str,
|
|
|
48 |
temperature: float = ChatConfig.DEFAULT_TEMP,
|
49 |
top_p: float = ChatConfig.DEFAULT_TOP_P
|
50 |
) -> str:
|
51 |
+
if not documents:
|
52 |
+
return "Please upload a PDF first."
|
53 |
+
|
54 |
+
context = search_relevant_text(message) # Get relevant content from PDF
|
55 |
+
|
56 |
messages = [{"role": "system", "content": system_message}]
|
|
|
|
|
57 |
for user_msg, bot_msg in history:
|
58 |
if user_msg:
|
59 |
messages.append({"role": "user", "content": user_msg})
|
60 |
if bot_msg:
|
61 |
messages.append({"role": "assistant", "content": bot_msg})
|
62 |
+
|
63 |
+
messages.append({"role": "user", "content": f"Context: {context}\nQuestion: {message}"})
|
64 |
+
|
65 |
response = ""
|
66 |
for chunk in client.chat_completion(
|
67 |
messages,
|
|
|
74 |
response += token
|
75 |
yield response
|
76 |
|
77 |
+
def handle_upload(pdf_file):
|
78 |
+
"""Handles PDF upload and creates vector DB"""
|
79 |
+
text_chunks = extract_text_from_pdf(pdf_file.name)
|
80 |
+
create_vector_db(text_chunks)
|
81 |
+
return "PDF uploaded and indexed successfully!"
|
82 |
|
83 |
+
def create_interface() -> gr.Blocks:
|
84 |
+
"""Creates the Gradio interface"""
|
85 |
+
with gr.Blocks() as interface:
|
86 |
+
gr.Markdown("# PDF-Based Chatbot using Google Gemma")
|
87 |
+
|
88 |
+
with gr.Row():
|
89 |
+
chatbot = gr.Chatbot(label="Chat with Your PDF")
|
90 |
+
pdf_upload = gr.File(label="Upload PDF", type="file")
|
91 |
+
|
92 |
+
with gr.Row():
|
93 |
+
user_input = gr.Textbox(label="Ask a question", placeholder="Type here...")
|
94 |
+
send_button = gr.Button("Send")
|
95 |
+
|
96 |
+
output = gr.Textbox(label="Response", lines=5)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
97 |
|
98 |
+
# Upload PDF handler
|
99 |
+
pdf_upload.change(handle_upload, inputs=[pdf_upload], outputs=[])
|
100 |
+
|
101 |
+
# Chat function
|
102 |
+
send_button.click(
|
103 |
+
generate_response,
|
104 |
+
inputs=[user_input, chatbot],
|
105 |
+
outputs=[output]
|
106 |
+
)
|
107 |
+
|
108 |
+
return interface
|
109 |
|
110 |
if __name__ == "__main__":
|
111 |
+
app = create_interface()
|
112 |
+
app.launch()
|