muhammadrazapathan commited on
Commit
2c9888e
Β·
verified Β·
1 Parent(s): f211fe2

Delete app.py

Browse files
Files changed (1) hide show
  1. app.py +0 -111
app.py DELETED
@@ -1,111 +0,0 @@
1
- import os
2
- import gradio as gr
3
- import tempfile
4
- from dotenv import load_dotenv
5
-
6
- from langchain_community.document_loaders import PyPDFLoader
7
- from langchain_text_splitters import RecursiveCharacterTextSplitter
8
- from langchain_huggingface import HuggingFaceEmbeddings
9
- from langchain_community.vectorstores import FAISS
10
-
11
- from groq import Groq
12
-
13
- # ================== ENVIRONMENT ==================
14
- load_dotenv()
15
- GROQ_API_KEY = os.getenv("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")
16
-
17
- if not GROQ_API_KEY:
18
- raise ValueError("❌ GROQ_API_KEY not found. Please set it in environment variables.")
19
-
20
- client = Groq(api_key=GROQ_API_KEY)
21
-
22
- # ================== GLOBAL DATABASE ==================
23
- vector_db = None
24
-
25
- # ================== LLM FUNCTION ==================
26
- def groq_llm(prompt):
27
- response = client.chat.completions.create(
28
- model="llama-3.3-70b-versatile",
29
- messages=[{"role": "user", "content": prompt}],
30
- )
31
- return response.choices[0].message.content
32
-
33
- # ================== PDF PROCESSING ==================
34
- def process_pdf(file):
35
- global vector_db
36
-
37
- if file is None:
38
- return "❌ Please upload a PDF file."
39
-
40
- # Save file temporarily
41
- with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as tmp:
42
- tmp.write(file)
43
- pdf_path = tmp.name
44
-
45
- # Load PDF
46
- loader = PyPDFLoader(pdf_path)
47
- documents = loader.load()
48
-
49
- # Chunking
50
- splitter = RecursiveCharacterTextSplitter(
51
- chunk_size=500,
52
- chunk_overlap=100
53
- )
54
- docs = splitter.split_documents(documents)
55
-
56
- # Embeddings (open-source)
57
- embeddings = HuggingFaceEmbeddings(
58
- model_name="sentence-transformers/all-MiniLM-L6-v2"
59
- )
60
-
61
- # Vector store
62
- vector_db = FAISS.from_documents(docs, embeddings)
63
-
64
- return f"βœ… Document processed successfully! {len(docs)} chunks created."
65
-
66
- # ================== QUESTION ANSWERING ==================
67
- def ask_question(question):
68
- global vector_db
69
-
70
- if vector_db is None:
71
- return "❌ Please upload and process a document first."
72
-
73
- retriever = vector_db.as_retriever(search_kwargs={"k": 3})
74
- docs = retriever.get_relevant_documents(question)
75
-
76
- context = "\n\n".join([doc.page_content for doc in docs])
77
-
78
- prompt = f"""
79
- You are an intelligent assistant. Use the following context to answer the user's question.
80
-
81
- Context:
82
- {context}
83
-
84
- Question:
85
- {question}
86
-
87
- Answer:
88
- """
89
-
90
- answer = groq_llm(prompt)
91
- return answer
92
-
93
- # ================== GRADIO UI ==================
94
- with gr.Blocks(title="πŸ“„ RAG PDF Question Answering App") as demo:
95
- gr.Markdown("## πŸ“„ RAG (Retrieval-Augmented Generation) Application")
96
- gr.Markdown("Upload a PDF document and ask questions about its content.")
97
-
98
- with gr.Row():
99
- pdf_upload = gr.File(label="Upload PDF", file_types=[".pdf"])
100
- process_btn = gr.Button("πŸ“₯ Process Document")
101
-
102
- status = gr.Textbox(label="Status", interactive=False)
103
-
104
- with gr.Row():
105
- question = gr.Textbox(label="Ask a Question", placeholder="Type your question here...")
106
- answer = gr.Textbox(label="Answer", interactive=False)
107
-
108
- process_btn.click(fn=process_pdf, inputs=pdf_upload, outputs=status)
109
- question.submit(fn=ask_question, inputs=question, outputs=answer)
110
-
111
- demo.launch()