cstr commited on
Commit
cb22f6c
Β·
verified Β·
1 Parent(s): bfc0f42

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +98 -66
app.py CHANGED
@@ -31,13 +31,19 @@ class ModelRegistry:
31
  self.groq_models = self._fetch_groq_models()
32
 
33
  def _fetch_groq_models(self) -> Dict[str, str]:
34
- """Fetch available Groq models"""
35
  try:
 
 
 
 
 
36
  headers = {
37
- "Authorization": f"Bearer {os.getenv('GROQ_API_KEY')}",
38
  "Content-Type": "application/json"
39
  }
40
  response = requests.get("https://api.groq.com/openai/v1/models", headers=headers)
 
41
  if response.status_code == 200:
42
  models = response.json().get("data", [])
43
  return {model["id"]: model["id"] for model in models}
@@ -142,6 +148,38 @@ def build_prompts(snippets: List[str], prompt_instruction: str, custom_prompt: O
142
 
143
  return "\n\n".join(prompts)
144
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
145
  def send_to_hf_inference(prompt: str, model_name: str, api_key: str) -> str:
146
  """Send prompt to HuggingFace using Inference API"""
147
  try:
@@ -179,26 +217,27 @@ def send_to_groq(prompt: str, model_name: str, api_key: str) -> str:
179
  return f"Error with Groq API: {e}"
180
 
181
  def copy_to_clipboard(text: str) -> str:
182
- """Copy text to clipboard"""
183
- return "Text copied to clipboard!"
184
-
185
- def open_chatgpt() -> str:
186
- """Open ChatGPT in browser"""
187
- webbrowser.open('https://chat.openai.com/')
188
- return "Opening ChatGPT in browser..."
189
-
190
- def process_pdf(pdf, fmt, ctx_size, snippet_num, prompt, model_selection,
191
- hf_model_choice, hf_custom_model, hf_api_key,
192
- groq_model_choice, groq_api_key) -> Tuple[str, str, str, List[str]]:
193
- """Process PDF and generate summary"""
 
194
  try:
195
  if not pdf:
196
- return "Please upload a PDF file.", "", "", []
197
 
198
  # Extract text
199
  text = extract_text_from_pdf(pdf.name)
200
  if text.startswith("Error"):
201
- return text, "", "", []
202
 
203
  # Format content
204
  formatted_text = format_content(text, fmt)
@@ -211,42 +250,17 @@ def process_pdf(pdf, fmt, ctx_size, snippet_num, prompt, model_selection,
211
  full_prompt = build_prompts(snippets, default_prompt, prompt, snippet_num)
212
 
213
  if isinstance(full_prompt, str) and full_prompt.startswith("Error"):
214
- return full_prompt, "", "", []
215
-
216
- # Process with selected model
217
- if model_selection == "HuggingFace Inference":
218
- if not hf_api_key:
219
- return "HuggingFace API key required.", full_prompt, "", []
220
-
221
- model_id = hf_custom_model if hf_model_choice == "Custom Model" else model_registry.hf_models[hf_model_choice]
222
- summary = send_to_hf_inference(full_prompt, model_id, hf_api_key)
223
-
224
- elif model_selection == "Groq API":
225
- if not groq_api_key:
226
- return "Groq API key required.", full_prompt, "", []
227
-
228
- summary = send_to_groq(full_prompt, groq_model_choice, groq_api_key)
229
-
230
- else: # OpenAI ChatGPT
231
- summary = "Please use the Copy Prompt button and paste into ChatGPT."
232
-
233
- # Save files for download
234
- files_to_download = []
235
 
 
236
  with tempfile.NamedTemporaryFile(delete=False, mode='w', suffix='.txt') as prompt_file:
237
  prompt_file.write(full_prompt)
238
- files_to_download.append(prompt_file.name)
239
-
240
- if summary != "Please use the Copy Prompt button and paste into ChatGPT.":
241
- with tempfile.NamedTemporaryFile(delete=False, mode='w', suffix='.txt') as summary_file:
242
- summary_file.write(summary)
243
- files_to_download.append(summary_file.name)
244
 
245
- return "Processing complete!", full_prompt, summary, files_to_download
246
 
247
  except Exception as e:
248
  logging.error(f"Error processing PDF: {e}")
249
- return f"Error processing PDF: {str(e)}", "", "", []
250
 
251
  # Main Interface
252
  with gr.Blocks(theme=gr.themes.Default()) as demo:
@@ -273,12 +287,18 @@ with gr.Blocks(theme=gr.themes.Default()) as demo:
273
  label="πŸ“ Output Format"
274
  )
275
 
276
- gr.Markdown("### Context Window Size")
277
  with gr.Row():
278
- context_buttons = []
279
  for size_name, size_value in CONTEXT_SIZES.items():
280
- btn = gr.Button(size_name)
281
- context_buttons.append((btn, size_value))
 
 
 
 
 
 
 
282
 
283
  context_size = gr.Slider(
284
  minimum=1000,
@@ -334,6 +354,13 @@ with gr.Blocks(theme=gr.themes.Default()) as demo:
334
  type="password"
335
  )
336
 
 
 
 
 
 
 
 
337
  # Right Column - Output
338
  with gr.Column(scale=1):
339
  process_button = gr.Button("πŸš€ Process PDF", variant="primary")
@@ -370,7 +397,8 @@ with gr.Blocks(theme=gr.themes.Default()) as demo:
370
  def toggle_model_options(choice):
371
  return (
372
  gr.update(visible=choice == "HuggingFace Inference"),
373
- gr.update(visible=choice == "Groq API")
 
374
  )
375
 
376
  def refresh_groq_models_list():
@@ -384,7 +412,7 @@ with gr.Blocks(theme=gr.themes.Default()) as demo:
384
  model_choice.change(
385
  toggle_model_options,
386
  inputs=[model_choice],
387
- outputs=[hf_options, groq_options]
388
  )
389
 
390
  for btn, size_value in context_buttons:
@@ -412,17 +440,30 @@ with gr.Blocks(theme=gr.themes.Default()) as demo:
412
  format_type,
413
  context_size,
414
  snippet_number,
415
- custom_prompt,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
416
  model_choice,
417
  hf_model,
418
  hf_custom_model,
419
  hf_api_key,
420
  groq_model,
421
- groq_api_key
 
422
  ],
423
  outputs=[
424
- progress_status,
425
- generated_prompt,
426
  summary_output,
427
  download_files
428
  ]
@@ -451,22 +492,13 @@ with gr.Blocks(theme=gr.themes.Default()) as demo:
451
  1. Upload a PDF document
452
  2. Choose output format and context window size
453
  3. Select snippet number (default: 1) or enter custom prompt
454
- 4. Select your preferred model:
455
  - OpenAI ChatGPT: Manual copy/paste workflow
456
  - HuggingFace Inference: Direct API integration
457
  - Groq API: High-performance inference
458
  5. Click 'Process PDF' to generate summary
459
- 6. Use 'Copy Prompt' and 'Open ChatGPT' for manual processing
460
  7. Download generated files as needed
461
-
462
- ### βš™οΈ Features:
463
- - Support for multiple PDF formats
464
- - Flexible text formatting options
465
- - Predefined context window sizes (4K to 200K)
466
- - Multiple model integrations
467
- - Copy to clipboard functionality
468
- - Direct ChatGPT integration
469
- - Downloadable outputs
470
  """)
471
 
472
  # Launch the interface
 
31
  self.groq_models = self._fetch_groq_models()
32
 
33
  def _fetch_groq_models(self) -> Dict[str, str]:
34
+ """Fetch available Groq models with proper error handling"""
35
  try:
36
+ groq_api_key = os.getenv('GROQ_API_KEY')
37
+ if not groq_api_key:
38
+ logging.warning("No GROQ_API_KEY found in environment")
39
+ return self._get_default_groq_models()
40
+
41
  headers = {
42
+ "Authorization": f"Bearer {groq_api_key}",
43
  "Content-Type": "application/json"
44
  }
45
  response = requests.get("https://api.groq.com/openai/v1/models", headers=headers)
46
+
47
  if response.status_code == 200:
48
  models = response.json().get("data", [])
49
  return {model["id"]: model["id"] for model in models}
 
148
 
149
  return "\n\n".join(prompts)
150
 
151
+ def send_to_model(prompt, model_selection, hf_model_choice, hf_custom_model, hf_api_key,
152
+ groq_model_choice, groq_api_key, openai_api_key):
153
+ """Send prompt to selected model"""
154
+ try:
155
+ if model_selection == "HuggingFace Inference":
156
+ if not hf_api_key:
157
+ return "HuggingFace API key required.", []
158
+
159
+ model_id = hf_custom_model if hf_model_choice == "Custom Model" else model_registry.hf_models[hf_model_choice]
160
+ summary = send_to_hf_inference(prompt, model_id, hf_api_key)
161
+
162
+ elif model_selection == "Groq API":
163
+ if not groq_api_key:
164
+ return "Groq API key required.", []
165
+
166
+ summary = send_to_groq(prompt, groq_model_choice, groq_api_key)
167
+
168
+ elif model_selection == "OpenAI ChatGPT":
169
+ if not openai_api_key:
170
+ return "OpenAI API key required.", []
171
+ # Implement OpenAI API call here
172
+
173
+ # Save summary for download
174
+ with tempfile.NamedTemporaryFile(delete=False, mode='w', suffix='.txt') as summary_file:
175
+ summary_file.write(summary)
176
+
177
+ return summary, [summary_file.name]
178
+
179
+ except Exception as e:
180
+ logging.error(f"Error sending to model: {e}")
181
+ return f"Error sending to model: {str(e)}", []
182
+
183
  def send_to_hf_inference(prompt: str, model_name: str, api_key: str) -> str:
184
  """Send prompt to HuggingFace using Inference API"""
185
  try:
 
217
  return f"Error with Groq API: {e}"
218
 
219
  def copy_to_clipboard(text: str) -> str:
220
+ """Copy text to clipboard using JavaScript"""
221
+ return """
222
+ navigator.clipboard.writeText(text)
223
+ .then(() => gradioApp().querySelector('#progress_status').value = 'Copied to clipboard!')
224
+ .catch(() => gradioApp().querySelector('#progress_status').value = 'Failed to copy');
225
+ """
226
+
227
+ def open_chatgpt() -> None:
228
+ """Open ChatGPT in new browser tab"""
229
+ return """window.open('https://chat.openai.com/', '_blank');"""
230
+
231
+ def process_pdf(pdf, fmt, ctx_size, snippet_num, prompt):
232
+ """Generate prompt from PDF without model processing"""
233
  try:
234
  if not pdf:
235
+ return "Please upload a PDF file.", "", []
236
 
237
  # Extract text
238
  text = extract_text_from_pdf(pdf.name)
239
  if text.startswith("Error"):
240
+ return text, "", []
241
 
242
  # Format content
243
  formatted_text = format_content(text, fmt)
 
250
  full_prompt = build_prompts(snippets, default_prompt, prompt, snippet_num)
251
 
252
  if isinstance(full_prompt, str) and full_prompt.startswith("Error"):
253
+ return full_prompt, "", []
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
254
 
255
+ # Save prompt for download
256
  with tempfile.NamedTemporaryFile(delete=False, mode='w', suffix='.txt') as prompt_file:
257
  prompt_file.write(full_prompt)
 
 
 
 
 
 
258
 
259
+ return "Prompt generated!", full_prompt, [prompt_file.name]
260
 
261
  except Exception as e:
262
  logging.error(f"Error processing PDF: {e}")
263
+ return f"Error processing PDF: {str(e)}", "", []
264
 
265
  # Main Interface
266
  with gr.Blocks(theme=gr.themes.Default()) as demo:
 
287
  label="πŸ“ Output Format"
288
  )
289
 
290
+ gr.Markdown("### Context Size")
291
  with gr.Row():
 
292
  for size_name, size_value in CONTEXT_SIZES.items():
293
+ gr.Button(
294
+ size_name,
295
+ size="sm", # Make buttons smaller
296
+ scale=1 # Equal scaling
297
+ ).click(
298
+ lambda v=size_value: v,
299
+ None,
300
+ context_size
301
+ )
302
 
303
  context_size = gr.Slider(
304
  minimum=1000,
 
354
  type="password"
355
  )
356
 
357
+ # In the UI section, add OpenAI API key input:
358
+ with gr.Column(visible=False) as openai_options:
359
+ openai_api_key = gr.Textbox(
360
+ label="πŸ”‘ OpenAI API Key",
361
+ type="password"
362
+ )
363
+
364
  # Right Column - Output
365
  with gr.Column(scale=1):
366
  process_button = gr.Button("πŸš€ Process PDF", variant="primary")
 
397
  def toggle_model_options(choice):
398
  return (
399
  gr.update(visible=choice == "HuggingFace Inference"),
400
+ gr.update(visible=choice == "Groq API"),
401
+ gr.update(visible=choice == "OpenAI ChatGPT")
402
  )
403
 
404
  def refresh_groq_models_list():
 
412
  model_choice.change(
413
  toggle_model_options,
414
  inputs=[model_choice],
415
+ outputs=[hf_options, groq_options, openai_options]
416
  )
417
 
418
  for btn, size_value in context_buttons:
 
440
  format_type,
441
  context_size,
442
  snippet_number,
443
+ custom_prompt
444
+ ],
445
+ outputs=[
446
+ progress_status,
447
+ generated_prompt,
448
+ download_files
449
+ ]
450
+ )
451
+
452
+ # Add a new button for sending to model
453
+ send_button = gr.Button("πŸš€ Send to Model", variant="primary")
454
+ send_button.click(
455
+ send_to_model,
456
+ inputs=[
457
+ generated_prompt,
458
  model_choice,
459
  hf_model,
460
  hf_custom_model,
461
  hf_api_key,
462
  groq_model,
463
+ groq_api_key,
464
+ openai_api_key
465
  ],
466
  outputs=[
 
 
467
  summary_output,
468
  download_files
469
  ]
 
492
  1. Upload a PDF document
493
  2. Choose output format and context window size
494
  3. Select snippet number (default: 1) or enter custom prompt
495
+ 4. Select your preferred model in case you want to proceed directly (or continue with 5):
496
  - OpenAI ChatGPT: Manual copy/paste workflow
497
  - HuggingFace Inference: Direct API integration
498
  - Groq API: High-performance inference
499
  5. Click 'Process PDF' to generate summary
500
+ 6. Use 'Copy Prompt' and, optionally, 'Open ChatGPT' for manual processing
501
  7. Download generated files as needed
 
 
 
 
 
 
 
 
 
502
  """)
503
 
504
  # Launch the interface