Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,64 +1,92 @@
|
|
|
|
1 |
import gradio as gr
|
2 |
-
from
|
|
|
|
|
|
|
|
|
|
|
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 |
-
|
23 |
-
|
24 |
-
|
|
|
|
|
|
|
25 |
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
top_p=top_p,
|
36 |
-
):
|
37 |
-
token = message.choices[0].delta.content
|
38 |
-
|
39 |
-
response += token
|
40 |
-
yield response
|
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=[
|
49 |
-
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
50 |
-
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
51 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
52 |
-
gr.Slider(
|
53 |
-
minimum=0.1,
|
54 |
-
maximum=1.0,
|
55 |
-
value=0.95,
|
56 |
-
step=0.05,
|
57 |
-
label="Top-p (nucleus sampling)",
|
58 |
-
),
|
59 |
],
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
if __name__ == "__main__":
|
64 |
-
demo.launch()
|
|
|
1 |
+
import os
|
2 |
import gradio as gr
|
3 |
+
from langchain_community.document_loaders import PyMuPDFLoader, TextLoader
|
4 |
+
from langchain_text_splitters import CharacterTextSplitter
|
5 |
+
from langchain_community.vectorstores import FAISS
|
6 |
+
from langchain_community.embeddings import HuggingFaceEmbeddings
|
7 |
+
from langchain.chains import RetrievalQA
|
8 |
+
from transformers import pipeline, AutoTokenizer
|
9 |
|
10 |
+
def load_documents(file_path="file.pdf"):
|
11 |
+
# Supports both PDF and TXT files
|
12 |
+
documents = []
|
13 |
+
for filename in os.listdir(file_path):
|
14 |
+
path = os.path.join(file_path, filename)
|
15 |
+
if filename.endswith(".pdf"):
|
16 |
+
loader = PyMuPDFLoader(path)
|
17 |
+
documents.extend(loader.load())
|
18 |
+
elif filename.endswith(".txt"):
|
19 |
+
loader = TextLoader(path)
|
20 |
+
documents.extend(loader.load())
|
21 |
+
return documents
|
22 |
|
23 |
+
def create_qa_system():
|
24 |
+
try:
|
25 |
+
# 1. Load study materials
|
26 |
+
documents = load_documents()
|
27 |
+
if not documents:
|
28 |
+
raise ValueError("📚 No PDF/TXT files found in 'study_materials' folder")
|
29 |
+
|
30 |
+
# 2. Smart text splitting for educational content
|
31 |
+
text_splitter = CharacterTextSplitter(
|
32 |
+
chunk_size=800, # Optimized for textbook content
|
33 |
+
chunk_overlap=100,
|
34 |
+
separator="\n\n" # Preserve paragraph structure
|
35 |
+
)
|
36 |
+
texts = text_splitter.split_documents(documents)
|
37 |
+
|
38 |
+
# 3. Educational-focused embeddings
|
39 |
+
embeddings = HuggingFaceEmbeddings(
|
40 |
+
model_name="sentence-transformers/all-MiniLM-L6-v2"
|
41 |
+
)
|
42 |
+
|
43 |
+
# 4. Create knowledge base
|
44 |
+
db = FAISS.from_documents(texts, embeddings)
|
45 |
+
|
46 |
+
# 5. Configure student-friendly AI
|
47 |
+
qa_pipeline = pipeline(
|
48 |
+
"text2text-generation",
|
49 |
+
model="google/flan-t5-base",
|
50 |
+
tokenizer=AutoTokenizer.from_pretrained("google/flan-t5-base"),
|
51 |
+
max_length=300, # Longer answers for explanations
|
52 |
+
temperature=0.3, # Balance creativity/facts
|
53 |
+
device=-1 # Force CPU usage
|
54 |
+
)
|
55 |
+
|
56 |
+
return RetrievalQA.from_chain_type(
|
57 |
+
llm=qa_pipeline,
|
58 |
+
chain_type="stuff",
|
59 |
+
retriever=db.as_retriever(search_kwargs={"k": 2}),
|
60 |
+
return_source_documents=True
|
61 |
+
)
|
62 |
+
except Exception as e:
|
63 |
+
raise gr.Error(f"🚨 Study Assistant Setup Failed: {str(e)}")
|
64 |
|
65 |
+
# Initialize system
|
66 |
+
try:
|
67 |
+
qa = create_qa_system()
|
68 |
+
except Exception as e:
|
69 |
+
print(f"Critical Error: {str(e)}")
|
70 |
+
raise
|
|
|
|
|
|
|
71 |
|
72 |
+
def ask_question(question, history):
|
73 |
+
try:
|
74 |
+
result = qa({"query": question})
|
75 |
+
answer = result["result"]
|
76 |
+
sources = list({doc.metadata['source'] for doc in result['source_documents']})
|
77 |
+
return f"{answer}\n\n📚 Sources: {', '.join(sources)}"
|
78 |
+
except Exception as e:
|
79 |
+
return f"❌ Error: {str(e)[:150]}"
|
80 |
|
81 |
+
# Student-friendly interface
|
82 |
+
gr.ChatInterface(
|
83 |
+
ask_question,
|
84 |
+
title="Study Buddy AI",
|
85 |
+
description="Ask questions about your course materials!",
|
86 |
+
examples=[
|
87 |
+
"Explain the key points from Chapter 3",
|
88 |
+
"What's the difference between mitosis and meiosis?",
|
89 |
+
"List the main causes of World War II"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
90 |
],
|
91 |
+
theme="soft"
|
92 |
+
).launch()
|
|
|
|
|
|