AI-trainer1 commited on
Commit
e615195
Β·
verified Β·
1 Parent(s): 7fc377e

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +166 -0
app.py ADDED
@@ -0,0 +1,166 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # [1] Core Imports (Updated Packages)
2
+ import gradio as gr
3
+ from langchain_huggingface import HuggingFaceEmbeddings
4
+ from langchain_huggingface import HuggingFacePipeline
5
+ from langchain_community.document_loaders import UnstructuredURLLoader
6
+ from langchain.text_splitter import RecursiveCharacterTextSplitter
7
+ from langchain_chroma import Chroma
8
+ from langchain.chains import create_retrieval_chain
9
+ from langchain.chains.combine_documents.stuff import create_stuff_documents_chain
10
+ from langchain_core.prompts import ChatPromptTemplate
11
+ from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, pipeline
12
+ import nltk
13
+ import validators
14
+
15
+ nltk.download('punkt', quiet=True)
16
+
17
+ # [2] Initialize Components
18
+ text_splitter = RecursiveCharacterTextSplitter(
19
+ chunk_size=1000,
20
+ chunk_overlap=100,
21
+ separators=["\n\n", "\n"]
22
+ )
23
+
24
+ # Updated embeddings initialization
25
+ embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
26
+
27
+ # [3] Model Setup
28
+ MODEL_NAME = "google/flan-t5-large"
29
+ tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
30
+ model = AutoModelForSeq2SeqLM.from_pretrained(MODEL_NAME)
31
+
32
+ pipe = pipeline(
33
+ "text2text-generation",
34
+ model=model,
35
+ tokenizer=tokenizer,
36
+ max_new_tokens=800,
37
+ temperature=0.6,
38
+ do_sample=True
39
+ )
40
+
41
+ # Updated pipeline wrapper
42
+ llm = HuggingFacePipeline(pipeline=pipe)
43
+
44
+ # [4] Prompt Template
45
+ prompt_template = ChatPromptTemplate.from_messages([
46
+ ("system", "Generate a clear concise most simplest understanding language answer in about 3-5 bullet or more if you need more to explain points, using ONLY the context below.\n\nContext: {context}"),
47
+ ("human", "{input}")
48
+ ])
49
+
50
+ # [5] Processing Function
51
+ def process_inputs(urls_str, question):
52
+ try:
53
+ print("\n=== New Request ===")
54
+
55
+ # Validate inputs
56
+ if not urls_str.strip() or not question.strip():
57
+ print("Missing inputs")
58
+ return "❌ Please provide both URLs and a question"
59
+
60
+ urls = [url.strip() for url in urls_str.split(',') if url.strip()]
61
+ print(f"Processing {len(urls)} URLs")
62
+
63
+ # Validate URLs
64
+ for url in urls:
65
+ if not validators.url(url):
66
+ print(f"Invalid URL: {url}")
67
+ return f"❌ Invalid URL format: {url}"
68
+
69
+ # Load documents
70
+ try:
71
+ loader = UnstructuredURLLoader(urls=urls)
72
+ docs = loader.load()
73
+ print(f"Loaded {len(docs)} documents")
74
+ except Exception as e:
75
+ print(f"Document load failed: {str(e)}")
76
+ return f"❌ Failed to load documents: {str(e)}"
77
+
78
+ if not docs:
79
+ print("No content found")
80
+ return "❌ No content found in the provided URLs"
81
+
82
+ # Process documents
83
+ unique_content = list({doc.page_content.strip(): doc for doc in docs}.values())
84
+ split_docs = text_splitter.split_documents(unique_content)
85
+ print(f"Split into {len(split_docs)} chunks")
86
+
87
+ # Create vector store
88
+ try:
89
+ vectorstore = Chroma.from_documents(
90
+ documents=split_docs,
91
+ embedding=embeddings
92
+ )
93
+ retriever = vectorstore.as_retriever(search_kwargs={"k": 5})
94
+ print("Vector store created")
95
+ except Exception as e:
96
+ print(f"Vector store error: {str(e)}")
97
+ return f"❌ Vector store error: {str(e)}"
98
+
99
+ # Create chain
100
+ try:
101
+ print("Creating RAG chain")
102
+ rag_chain = create_retrieval_chain(
103
+ retriever,
104
+ create_stuff_documents_chain(
105
+ llm=llm,
106
+ prompt=prompt_template
107
+ )
108
+ )
109
+
110
+ print(f"Processing question: {question}")
111
+ response = rag_chain.invoke({"input": question})
112
+ print("Answer generated successfully")
113
+
114
+ return response["answer"]
115
+
116
+ except Exception as e:
117
+ print(f"Generation error: {str(e)}")
118
+ return f"❌ Generation error: {str(e)}"
119
+
120
+ except Exception as e:
121
+ print(f"Unexpected error: {str(e)}")
122
+ return f"❌ Unexpected error: {str(e)}"
123
+
124
+ # [6] Gradio Interface (Fixed parameters)
125
+ with gr.Blocks(theme=gr.themes.Soft()) as demo:
126
+ gr.Markdown("# RAG Chat Interface")
127
+
128
+ with gr.Row():
129
+ with gr.Column():
130
+ url_input = gr.Textbox(
131
+ label="Paste URLs (comma-separated)",
132
+ placeholder="https://example.com, https://another-site.org\nSome websites may not work as they won't allow to fetch data from their site.\nTry other websites in that case.",
133
+ lines=3
134
+ )
135
+ question_input = gr.Textbox(
136
+ label="Your Question",
137
+ placeholder="Type your question here...",
138
+ lines=3
139
+ )
140
+ submit_btn = gr.Button("Get Answer", variant="primary")
141
+
142
+ answer_output = gr.Textbox(
143
+ label="Generated Answer",
144
+ interactive=False,
145
+ lines=10 # Removed autoscroll=True
146
+ )
147
+
148
+ gr.Examples(
149
+ examples=[
150
+ [
151
+ "https://generativeai.net/, https://www.ibm.com/think/topics/generative-ai",
152
+ "What are the key benefits of generative AI?"
153
+ ]
154
+ ],
155
+ inputs=[url_input, question_input]
156
+ )
157
+
158
+ submit_btn.click(
159
+ fn=process_inputs,
160
+ inputs=[url_input, question_input],
161
+ outputs=answer_output
162
+ )
163
+
164
+ # [7] Launch
165
+ if __name__ == "__main__":
166
+ demo.launch()