mannir commited on
Commit
bd8906d
·
verified ·
1 Parent(s): a55aee3

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +122 -0
app.py ADDED
@@ -0,0 +1,122 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import tempfile
3
+ import gradio as gr
4
+ from langchain_community.embeddings import HuggingFaceEmbeddings
5
+ from langchain_community.document_loaders import PyPDFLoader
6
+ from langchain.text_splitter import CharacterTextSplitter
7
+ from langchain.chains.retrieval_qa.base import RetrievalQA
8
+ from langchain.prompts import PromptTemplate
9
+ from langchain_community.vectorstores import Chroma
10
+ from langchain.llms.base import LLM
11
+ from groq import Groq
12
+ from typing import Any, List, Optional
13
+
14
+ # Set up Groq client
15
+ GROQ_API_KEY = "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"
16
+ os.environ["GROQ_API_KEY"] = GROQ_API_KEY
17
+ groq_client = Groq(api_key=GROQ_API_KEY)
18
+
19
+ # Custom LLM class for Groq
20
+ class GroqLLM(LLM):
21
+ client: Any
22
+ model: str = "llama3-8b-8192"
23
+
24
+ def _call(self, prompt: str, stop: Optional[List[str]] = None) -> str:
25
+ chat_completion = self.client.chat.completions.create(
26
+ messages=[{"role": "user", "content": prompt}],
27
+ model=self.model,
28
+ )
29
+ return chat_completion.choices[0].message.content
30
+
31
+ @property
32
+ def _llm_type(self) -> str:
33
+ return "groq"
34
+
35
+ # Initialize GroqLLM
36
+ llm = GroqLLM(client=groq_client)
37
+
38
+ # Custom prompt template
39
+ template = """You are a direct and concise assistant. Answer the question using only the information provided in the context. Give only the specific answer requested, with no additional explanation or information.
40
+
41
+ Context: {context}
42
+
43
+ Question: {question}
44
+
45
+ Answer:"""
46
+
47
+ PROMPT = PromptTemplate(
48
+ template=template, input_variables=["context", "question"]
49
+ )
50
+
51
+ class PDFQuestionAnswering:
52
+ def __init__(self):
53
+ self.qa_system = None
54
+
55
+ def setup_qa_system(self, pdf_file):
56
+ # Create a temporary file
57
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as temp_file:
58
+ temp_file.write(pdf_file)
59
+ temp_file_path = temp_file.name
60
+
61
+ # Load PDF
62
+ loader = PyPDFLoader(temp_file_path)
63
+ documents = loader.load()
64
+
65
+ # Remove the temporary file
66
+ os.unlink(temp_file_path)
67
+
68
+ # Text splitting
69
+ text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)
70
+ texts = text_splitter.split_documents(documents)
71
+
72
+ # Embeddings
73
+ embeddings = HuggingFaceEmbeddings()
74
+
75
+ # Vector store
76
+ docsearch = Chroma.from_documents(texts, embeddings)
77
+
78
+ # Set up RetrievalQA
79
+ self.qa_system = RetrievalQA.from_chain_type(
80
+ llm=llm,
81
+ chain_type="stuff",
82
+ retriever=docsearch.as_retriever(),
83
+ chain_type_kwargs={"prompt": PROMPT}
84
+ )
85
+
86
+ return "PDF processed successfully. You can now ask questions."
87
+
88
+ def get_answer(self, question):
89
+ if self.qa_system is None:
90
+ return "Please upload a PDF file first."
91
+
92
+ raw_answer = self.qa_system.run(question)
93
+ return raw_answer.strip()
94
+
95
+ pdf_qa = PDFQuestionAnswering()
96
+
97
+ def process_pdf(pdf_file):
98
+ if pdf_file is None:
99
+ return "Please upload a PDF file."
100
+ return pdf_qa.setup_qa_system(pdf_file)
101
+
102
+ def answer_question(question):
103
+ return pdf_qa.get_answer(question)
104
+
105
+ # Gradio interface
106
+ with gr.Blocks() as demo:
107
+ gr.Markdown("# GROQ and LLAMA-3 Custom RAG Bot ")
108
+ with gr.Row():
109
+ pdf_input = gr.File(label="Upload PDF", type="binary", file_types=[".pdf"])
110
+ pdf_output = gr.Textbox(label="PDF Processing Status")
111
+ pdf_button = gr.Button("Process PDF")
112
+
113
+ with gr.Row():
114
+ question_input = gr.Textbox(label="Enter your question")
115
+ answer_output = gr.Textbox(label="Answer")
116
+ question_button = gr.Button("Get Answer")
117
+
118
+ pdf_button.click(process_pdf, inputs=[pdf_input], outputs=[pdf_output])
119
+ question_button.click(answer_question, inputs=[question_input], outputs=[answer_output])
120
+
121
+ if __name__ == "__main__":
122
+ demo.launch()