khurrameycon commited on
Commit
3ce5085
·
verified ·
1 Parent(s): 5e5c791

input_text with PDF

Browse files
Files changed (1) hide show
  1. app.py +25 -3
app.py CHANGED
@@ -4,6 +4,7 @@ import torch
4
  from transformers import AutoProcessor, MllamaForConditionalGeneration
5
  from PIL import Image
6
  import spaces
 
7
 
8
  # Check if we're running in a Hugging Face Space and if SPACES_ZERO_GPU is enabled
9
  IS_SPACES_ZERO = os.environ.get("SPACES_ZERO_GPU", "0") == "1"
@@ -54,17 +55,38 @@ processor = AutoProcessor.from_pretrained(model_name, use_auth_token=HF_TOKEN)
54
  # # Decode the output to return the final response
55
  # response = processor.decode(outputs[0], skip_special_tokens=True)
56
  # return response
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
57
  @spaces.GPU
58
- def predict_text(text):
 
 
59
  # Prepare the input messages
60
- messages = [{"role": "user", "content": [{"type": "text", "text": text}]}]
61
 
62
  # Create the input text using the processor's chat template
63
  input_text = processor.apply_chat_template(messages, add_generation_prompt=True)
64
 
65
  # Process the inputs and move to the appropriate device
66
  # inputs = processor(image, input_text, return_tensors="pt").to(device)
67
- inputs = processor(text=text, return_tensors="pt").to("cuda")
68
  # Generate a response from the model
69
  outputs = model.generate(**inputs, max_new_tokens=1024)
70
 
 
4
  from transformers import AutoProcessor, MllamaForConditionalGeneration
5
  from PIL import Image
6
  import spaces
7
+ import tempfile
8
 
9
  # Check if we're running in a Hugging Face Space and if SPACES_ZERO_GPU is enabled
10
  IS_SPACES_ZERO = os.environ.get("SPACES_ZERO_GPU", "0") == "1"
 
55
  # # Decode the output to return the final response
56
  # response = processor.decode(outputs[0], skip_special_tokens=True)
57
  # return response
58
+
59
+ def extract_text_from_pdf(pdf_url):
60
+ try:
61
+ response = requests.get(pdf_url)
62
+ response.raise_for_status()
63
+ with tempfile.NamedTemporaryFile(delete=False) as temp_pdf:
64
+ temp_pdf.write(response.content)
65
+ temp_pdf_path = temp_pdf.name
66
+
67
+ reader = PdfReader(temp_pdf_path)
68
+ text = ""
69
+ for page in reader.pages:
70
+ text += page.extract_text()
71
+
72
+ os.remove(temp_pdf_path)
73
+ return text
74
+ except Exception as e:
75
+ raise HTTPException(status_code=400, detail=f"Error extracting text from PDF: {str(e)}")
76
+
77
  @spaces.GPU
78
+ def predict_text(text, url = 'https://arinsight.co/2024_FA_AEC_1200_GR1_GR2.pdf'):
79
+ pdf_text = extract_text_from_pdf(url)
80
+ text_combined = text + "\n\nExtracted Text from PDF:\n" + pdf_text
81
  # Prepare the input messages
82
+ messages = [{"role": "user", "content": [{"type": "text", "text": text_combined}]}]
83
 
84
  # Create the input text using the processor's chat template
85
  input_text = processor.apply_chat_template(messages, add_generation_prompt=True)
86
 
87
  # Process the inputs and move to the appropriate device
88
  # inputs = processor(image, input_text, return_tensors="pt").to(device)
89
+ inputs = processor(text=input_text, return_tensors="pt").to("cuda")
90
  # Generate a response from the model
91
  outputs = model.generate(**inputs, max_new_tokens=1024)
92