claytonsds commited on
Commit
7a1c90d
·
verified ·
1 Parent(s): 935b88c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -37
app.py CHANGED
@@ -13,20 +13,18 @@ from langchain_core.output_parsers import StrOutputParser
13
  print("Token:", os.environ.get("HUGGINGFACEHUB_API_TOKEN"))
14
  token = os.environ.get("HUGGINGFACEHUB_API_TOKEN")
15
 
 
16
  # ------------------------
17
  # LLM Model (LLaMA 2)
18
  # ------------------------
19
- # Initialize the HuggingFace text-generation pipeline
20
  pipe = pipeline(
21
  task="text-generation",
22
  model="meta-llama/Llama-2-7b-hf",
23
- token=token, # <- aqui você passa o token
24
  temperature=0.7,
25
  max_new_tokens=512,
26
  device_map="auto"
27
  )
28
 
29
- # Wrap the pipeline into a LangChain LLM object
30
  llm = HuggingFacePipeline(
31
  pipeline=pipe,
32
  model_kwargs={"temperature": 0.7}
@@ -35,7 +33,6 @@ llm = HuggingFacePipeline(
35
  # ------------------------
36
  # Prompt template
37
  # ------------------------
38
- # Define a template for asking questions based on documents
39
  prompt = PromptTemplate.from_template(
40
  """Given the following extracted parts of a long document and a question, create a final answer with references.
41
  If you don't know the answer, just say that you don't know.
@@ -43,72 +40,61 @@ Question: {question}"""
43
  )
44
 
45
  # Global variable to store the QA chain
46
- qa_chain = None
47
 
48
  # ------------------------
49
- # Function to process URLs
50
  # ------------------------
51
- def process_urls(url1, url2, url3):
52
- global qa_chain
53
 
54
- # Collect non-empty URLs
55
  urls = [url1, url2, url3]
56
  urls = [u for u in urls if u.strip() != ""]
57
 
58
  if len(urls) == 0:
59
- return "⚠️ Please provide at least one URL."
 
60
 
61
- # Load documents from the URLs
62
  loader = UnstructuredURLLoader(urls=urls)
63
  documents = loader.load()
64
 
65
- # Split documents into smaller chunks for embedding
66
  text_splitter = RecursiveCharacterTextSplitter(
67
  chunk_size=600,
68
  chunk_overlap=200
69
  )
70
  splits = text_splitter.split_documents(documents)
71
 
72
- # Create embeddings for each chunk
73
  embeddings = HuggingFaceEmbeddings(
74
  model_name="mixedbread-ai/mxbai-embed-large-v1"
75
  )
76
 
77
- # Build a vectorstore (FAISS) from the document embeddings
78
  vectorstore = FAISS.from_documents(
79
  documents=splits,
80
  embedding=embeddings
81
  )
82
 
83
- # Create a retriever to fetch relevant document chunks
84
  retriever = vectorstore.as_retriever()
85
 
86
- # Create a simple QA chain using the prompt and LLM
87
  from langchain_core.runnables import RunnableSequence
88
  simple_chain = RunnableSequence(prompt, llm, StrOutputParser())
89
 
90
- # Store the retriever and chain in a global variable
91
- qa_chain = {"retriever": retriever, "chain": simple_chain}
92
-
93
- return "✅ URLs processed successfully!"
94
 
95
  # ------------------------
96
  # Function to answer questions
97
  # ------------------------
98
  def ask_question(question):
99
- global qa_chain
100
 
101
- if qa_chain is None:
102
  return "⚠️ Please process URLs first."
103
-
104
- # Retrieve the most relevant documents for the question
105
- docs = qa_chain["retriever"].get_relevant_documents(question)
106
 
107
- # Combine document texts into a single string for the prompt
108
- combined_text = "\n\n".join([d.page_content for d in docs])
109
-
110
- # Run the QA chain with the question and context
111
- result = qa_chain["chain"].invoke({"question": question, "context": combined_text})
112
  return result
113
 
114
  # ------------------------
@@ -116,7 +102,6 @@ def ask_question(question):
116
  # ------------------------
117
  with gr.Blocks() as app:
118
  with gr.Row():
119
-
120
  # Sidebar: URL input and processing
121
  with gr.Column(scale=1):
122
  gr.Markdown("## 📌 Insert URLs")
@@ -126,8 +111,8 @@ with gr.Blocks() as app:
126
  url3 = gr.Textbox(label="URL 3")
127
 
128
  process_btn = gr.Button("Process URLs")
129
- status_output = gr.Textbox(label="Status")
130
-
131
  # Main Area: Question input and answer output
132
  with gr.Column(scale=2):
133
  gr.Markdown("## ✍️ Write your question")
@@ -140,14 +125,15 @@ with gr.Blocks() as app:
140
 
141
  ask_btn = gr.Button("Ask")
142
  answer_output = gr.Textbox(label="Answer", lines=8)
143
-
144
  # Connect buttons to their functions
145
  process_btn.click(
146
- process_urls,
147
  inputs=[url1, url2, url3],
148
- outputs=status_output
 
149
  )
150
-
151
  ask_btn.click(
152
  ask_question,
153
  inputs=question_box,
 
13
  print("Token:", os.environ.get("HUGGINGFACEHUB_API_TOKEN"))
14
  token = os.environ.get("HUGGINGFACEHUB_API_TOKEN")
15
 
16
+
17
  # ------------------------
18
  # LLM Model (LLaMA 2)
19
  # ------------------------
 
20
  pipe = pipeline(
21
  task="text-generation",
22
  model="meta-llama/Llama-2-7b-hf",
 
23
  temperature=0.7,
24
  max_new_tokens=512,
25
  device_map="auto"
26
  )
27
 
 
28
  llm = HuggingFacePipeline(
29
  pipeline=pipe,
30
  model_kwargs={"temperature": 0.7}
 
33
  # ------------------------
34
  # Prompt template
35
  # ------------------------
 
36
  prompt = PromptTemplate.from_template(
37
  """Given the following extracted parts of a long document and a question, create a final answer with references.
38
  If you don't know the answer, just say that you don't know.
 
40
  )
41
 
42
  # Global variable to store the QA chain
43
+ simple_chain = None
44
 
45
  # ------------------------
46
+ # Function to process URLs with real-time logging
47
  # ------------------------
48
+ def process_urls_with_logs(url1, url2, url3):
49
+ global simple_chain
50
 
 
51
  urls = [url1, url2, url3]
52
  urls = [u for u in urls if u.strip() != ""]
53
 
54
  if len(urls) == 0:
55
+ yield "⚠️ Please provide at least one URL."
56
+ return
57
 
58
+ yield "⏳ Loading URLs..."
59
  loader = UnstructuredURLLoader(urls=urls)
60
  documents = loader.load()
61
 
62
+ yield "⏳ Creating the chunks..."
63
  text_splitter = RecursiveCharacterTextSplitter(
64
  chunk_size=600,
65
  chunk_overlap=200
66
  )
67
  splits = text_splitter.split_documents(documents)
68
 
69
+ yield "⏳ Creating embeddings..."
70
  embeddings = HuggingFaceEmbeddings(
71
  model_name="mixedbread-ai/mxbai-embed-large-v1"
72
  )
73
 
74
+ yield "⏳ Creating a vector database-like structure (FAISS)..."
75
  vectorstore = FAISS.from_documents(
76
  documents=splits,
77
  embedding=embeddings
78
  )
79
 
80
+ yield "⏳ Starting LLM model (meta-llama/Llama-2-7b-hf)..."
81
  retriever = vectorstore.as_retriever()
82
 
 
83
  from langchain_core.runnables import RunnableSequence
84
  simple_chain = RunnableSequence(prompt, llm, StrOutputParser())
85
 
86
+ yield "✅ URLs processed successfully!"
 
 
 
87
 
88
  # ------------------------
89
  # Function to answer questions
90
  # ------------------------
91
  def ask_question(question):
92
+ global simple_chain
93
 
94
+ if simple_chain is None:
95
  return "⚠️ Please process URLs first."
 
 
 
96
 
97
+ result = simple_chain.invoke({"question": question})
 
 
 
 
98
  return result
99
 
100
  # ------------------------
 
102
  # ------------------------
103
  with gr.Blocks() as app:
104
  with gr.Row():
 
105
  # Sidebar: URL input and processing
106
  with gr.Column(scale=1):
107
  gr.Markdown("## 📌 Insert URLs")
 
111
  url3 = gr.Textbox(label="URL 3")
112
 
113
  process_btn = gr.Button("Process URLs")
114
+ status_output = gr.Textbox(label="Status", lines=8)
115
+
116
  # Main Area: Question input and answer output
117
  with gr.Column(scale=2):
118
  gr.Markdown("## ✍️ Write your question")
 
125
 
126
  ask_btn = gr.Button("Ask")
127
  answer_output = gr.Textbox(label="Answer", lines=8)
128
+
129
  # Connect buttons to their functions
130
  process_btn.click(
131
+ process_urls_with_logs,
132
  inputs=[url1, url2, url3],
133
+ outputs=status_output,
134
+ streaming=True # ⚡️ atualiza logs em tempo real
135
  )
136
+
137
  ask_btn.click(
138
  ask_question,
139
  inputs=question_box,