Ahmad-01 commited on
Commit
047bd57
·
verified ·
1 Parent(s): 26ff962

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -16
app.py CHANGED
@@ -5,9 +5,11 @@ from groq import Groq
5
  from sentence_transformers import SentenceTransformer
6
  from pypdf import PdfReader
7
  import gradio as gr
 
 
 
8
 
9
  # -------------------- Configuration --------------------
10
- # Get API key from environment variable (set as a secret on Hugging Face)
11
  GROQ_API_KEY = os.getenv("GROQ_API_KEY")
12
  if not GROQ_API_KEY:
13
  raise ValueError("GROQ_API_KEY environment variable not set.")
@@ -17,7 +19,6 @@ embedding_model = SentenceTransformer("all-MiniLM-L6-v2")
17
 
18
  # -------------------- PDF processing --------------------
19
  def extract_text_from_pdf(pdf_file):
20
- """Extract text from a PDF file."""
21
  reader = PdfReader(pdf_file)
22
  text = ""
23
  for page in reader.pages:
@@ -27,18 +28,15 @@ def extract_text_from_pdf(pdf_file):
27
  return text.strip()
28
 
29
  def chunk_text(text, chunk_size=800, overlap=150):
30
- """Split text into overlapping chunks."""
31
  chunks = []
32
  start = 0
33
  while start < len(text):
34
  end = start + chunk_size
35
- chunk = text[start:end]
36
- chunks.append(chunk)
37
  start = end - overlap
38
  return chunks
39
 
40
  def create_faiss_index(chunks):
41
- """Build a FAISS index from text chunks."""
42
  embeddings = embedding_model.encode(chunks)
43
  embeddings = np.array(embeddings).astype("float32")
44
  dimension = embeddings.shape[1]
@@ -47,7 +45,6 @@ def create_faiss_index(chunks):
47
  return index
48
 
49
  def retrieve(query, index, chunks, top_k=3):
50
- """Retrieve most relevant chunks for a query."""
51
  query_embedding = embedding_model.encode([query])
52
  query_embedding = np.array(query_embedding).astype("float32")
53
  distances, indices = index.search(query_embedding, top_k)
@@ -58,7 +55,6 @@ def retrieve(query, index, chunks, top_k=3):
58
  return retrieved
59
 
60
  def ask_llama(context, question):
61
- """Query Groq's Llama model with context."""
62
  prompt = f"""
63
  You are a pharmaceutical expert assistant.
64
 
@@ -81,12 +77,11 @@ Question:
81
  except Exception as e:
82
  return f"Groq API Error: {str(e)}"
83
 
84
- # -------------------- Global state (for demo simplicity) --------------------
85
  index = None
86
  chunks = None
87
 
88
  def process_pdf(pdf):
89
- """Upload and index a PDF."""
90
  global index, chunks
91
  if pdf is None:
92
  return "⚠️ Please upload a PDF."
@@ -98,7 +93,6 @@ def process_pdf(pdf):
98
  return "✅ Document processed successfully!"
99
 
100
  def chatbot(question):
101
- """Answer a question using the indexed document."""
102
  global index, chunks
103
  if index is None:
104
  return "⚠️ Please upload and process a PDF first."
@@ -136,7 +130,7 @@ textarea {
136
  }
137
  """
138
 
139
- with gr.Blocks(css=custom_css) as demo:
140
  gr.Markdown("""
141
  <h1 style='text-align:center; font-size:48px; font-weight:800;
142
  background: linear-gradient(90deg,#ff6a00,#ee0979,#667eea);
@@ -165,11 +159,9 @@ with gr.Blocks(css=custom_css) as demo:
165
  label="🧠 AI Answer",
166
  lines=18,
167
  max_lines=30,
168
- show_copy_button=True,
169
  interactive=False
170
  )
171
  ask_button.click(fn=chatbot, inputs=question_input, outputs=answer_output)
172
 
173
- # Launch the app (Hugging Face Spaces will call demo.launch() automatically)
174
- if __name__ == "__main__":
175
- demo.launch()
 
5
  from sentence_transformers import SentenceTransformer
6
  from pypdf import PdfReader
7
  import gradio as gr
8
+ import nest_asyncio
9
+
10
+ nest_asyncio.apply()
11
 
12
  # -------------------- Configuration --------------------
 
13
  GROQ_API_KEY = os.getenv("GROQ_API_KEY")
14
  if not GROQ_API_KEY:
15
  raise ValueError("GROQ_API_KEY environment variable not set.")
 
19
 
20
  # -------------------- PDF processing --------------------
21
  def extract_text_from_pdf(pdf_file):
 
22
  reader = PdfReader(pdf_file)
23
  text = ""
24
  for page in reader.pages:
 
28
  return text.strip()
29
 
30
  def chunk_text(text, chunk_size=800, overlap=150):
 
31
  chunks = []
32
  start = 0
33
  while start < len(text):
34
  end = start + chunk_size
35
+ chunks.append(text[start:end])
 
36
  start = end - overlap
37
  return chunks
38
 
39
  def create_faiss_index(chunks):
 
40
  embeddings = embedding_model.encode(chunks)
41
  embeddings = np.array(embeddings).astype("float32")
42
  dimension = embeddings.shape[1]
 
45
  return index
46
 
47
  def retrieve(query, index, chunks, top_k=3):
 
48
  query_embedding = embedding_model.encode([query])
49
  query_embedding = np.array(query_embedding).astype("float32")
50
  distances, indices = index.search(query_embedding, top_k)
 
55
  return retrieved
56
 
57
  def ask_llama(context, question):
 
58
  prompt = f"""
59
  You are a pharmaceutical expert assistant.
60
 
 
77
  except Exception as e:
78
  return f"Groq API Error: {str(e)}"
79
 
80
+ # -------------------- Global state --------------------
81
  index = None
82
  chunks = None
83
 
84
  def process_pdf(pdf):
 
85
  global index, chunks
86
  if pdf is None:
87
  return "⚠️ Please upload a PDF."
 
93
  return "✅ Document processed successfully!"
94
 
95
  def chatbot(question):
 
96
  global index, chunks
97
  if index is None:
98
  return "⚠️ Please upload and process a PDF first."
 
130
  }
131
  """
132
 
133
+ with gr.Blocks() as demo:
134
  gr.Markdown("""
135
  <h1 style='text-align:center; font-size:48px; font-weight:800;
136
  background: linear-gradient(90deg,#ff6a00,#ee0979,#667eea);
 
159
  label="🧠 AI Answer",
160
  lines=18,
161
  max_lines=30,
 
162
  interactive=False
163
  )
164
  ask_button.click(fn=chatbot, inputs=question_input, outputs=answer_output)
165
 
166
+ # Launch with custom CSS (Gradio 6+ compatible)
167
+ demo.launch(css=custom_css)