NaimaAqeel commited on
Commit
6cc8328
·
verified ·
1 Parent(s): 84f3457

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -14
app.py CHANGED
@@ -6,7 +6,7 @@ from sentence_transformers import SentenceTransformer
6
  import faiss
7
  import numpy as np
8
  import pickle
9
- from langchain_community.llms import HuggingFaceEndpoint
10
  from langchain_community.vectorstores import FAISS
11
  from langchain_community.embeddings import HuggingFaceEmbeddings
12
  import gradio as gr
@@ -23,7 +23,7 @@ if not api_token:
23
  raise ValueError("HUGGINGFACEHUB_API_TOKEN environment variable is not set")
24
  print(f"API Token: {api_token[:5]}...")
25
 
26
- # Initialize the HuggingFace LLM
27
  llm = HuggingFaceEndpoint(
28
  endpoint_url="https://api-inference.huggingface.co/models/gpt2",
29
  model_kwargs={"api_key": api_token}
@@ -61,8 +61,23 @@ def extract_text_from_docx(docx_path):
61
  return text
62
 
63
 
64
- def process_and_query(text):
65
- # Process the text and update FAISS index (similar to the previous code)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
66
  sentences = text.split("\n")
67
  embeddings = embedding_model.encode(sentences)
68
  index.add(np.array(embeddings))
@@ -76,23 +91,18 @@ def process_and_query(text):
76
  if idx != -1: # Ensure that a valid index is found
77
  top_documents.append(f"Document {idx}")
78
 
79
- # Generate response using LLM (optional)
80
  # You can replace this with your desired LLM interaction logic
81
- response = llm.run(inputs=text, max_length=100, temperature=0.7)["generated_text"]
82
 
83
- return {"top_documents": top_documents, "response": response}
84
 
85
 
86
  # Define the Gradio interface
87
  interface = gr.Interface(
88
  fn=process_and_query,
89
- inputs="textbox",
90
- outputs=["list", "text"],
91
- title="Chatbot with Text Processing and Retrieval",
92
- description="Upload a document (PDF or Word) or enter text to process. The chatbot will retrieve relevant documents and generate a response (optional).",
93
- )
94
 
95
- # Launch the Gradio interface
96
- interface.launch()
97
 
98
 
 
6
  import faiss
7
  import numpy as np
8
  import pickle
9
+ from langchain_community.llms import HuggingFaceEndpoint # Might need update
10
  from langchain_community.vectorstores import FAISS
11
  from langchain_community.embeddings import HuggingFaceEmbeddings
12
  import gradio as gr
 
23
  raise ValueError("HUGGINGFACEHUB_API_TOKEN environment variable is not set")
24
  print(f"API Token: {api_token[:5]}...")
25
 
26
+ # Initialize the HuggingFace LLM (Optional, comment out if not used)
27
  llm = HuggingFaceEndpoint(
28
  endpoint_url="https://api-inference.huggingface.co/models/gpt2",
29
  model_kwargs={"api_key": api_token}
 
61
  return text
62
 
63
 
64
+ def process_and_query(text, file=None):
65
+ # Check if a file is uploaded
66
+ if file:
67
+ # Get the uploaded file content
68
+ content = file.read()
69
+ if file.filename.endswith('.pdf'):
70
+ with open("temp.pdf", "wb") as f:
71
+ f.write(content)
72
+ text = extract_text_from_pdf("temp.pdf")
73
+ elif file.filename.endswith('.docx'):
74
+ with open("temp.docx", "wb") as f:
75
+ f.write(content)
76
+ text = extract_text_from_docx("temp.docx")
77
+ else:
78
+ return {"error": "Unsupported file format"}
79
+
80
+ # Process the text and update FAISS index (similar to previous code)
81
  sentences = text.split("\n")
82
  embeddings = embedding_model.encode(sentences)
83
  index.add(np.array(embeddings))
 
91
  if idx != -1: # Ensure that a valid index is found
92
  top_documents.append(f"Document {idx}")
93
 
94
+ # Generate response using LLM (optional, commented out)
95
  # You can replace this with your desired LLM interaction logic
96
+ # response = llm.run(inputs=text, max_length=100, temperature=0.7)["generated_text"]
97
 
98
+ return {"top_documents": top_documents, "response": None} # Response from LLM (optional)
99
 
100
 
101
  # Define the Gradio interface
102
  interface = gr.Interface(
103
  fn=process_and_query,
104
+ inputs={"text": gr.Textbox(label="Enter text or upload a file"),
105
+ "file": gr.FileUpload(label="Upload File (PDF or Word
 
 
 
106
 
 
 
107
 
108