khurrameycon commited on
Commit
b5c4f55
·
verified ·
1 Parent(s): ea3c5b4

Image added

Browse files
Files changed (1) hide show
  1. app.py +64 -1
app.py CHANGED
@@ -62,6 +62,56 @@ processor = AutoProcessor.from_pretrained(model_name, use_auth_token=HF_TOKEN)
62
  # response = processor.decode(outputs[0], skip_special_tokens=True)
63
  # return response
64
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
65
  def extract_text_from_pdf(pdf_url):
66
  try:
67
  response = requests.get(pdf_url)
@@ -128,6 +178,11 @@ PROMPT = (
128
  PROMPT_SKILLS = (
129
  "Extract the Course name and Primary Skills from this text. "
130
  )
 
 
 
 
 
131
  @app.route("/", methods=["GET"])
132
  def home():
133
  return jsonify({"message": "Welcome to the PDF Extraction API. Use the /extract endpoint to extract information."})
@@ -147,10 +202,18 @@ def extract_info():
147
  pdf_text = extract_text_from_pdf(pdf_url)
148
  prompt = f"{PROMPT}\n\n{pdf_text}"
149
  response = predict_text(prompt)
 
150
  if data["skills"] == True:
151
  prompt_skills = f"{PROMPT_SKILLS}\n\n{pdf_text}"
152
  response_skills = predict_text(prompt_skills)
153
- return jsonify({"extracted_info": response + response_skills})
 
 
 
 
 
 
 
154
  except Exception as e:
155
  return jsonify({"error": str(e)}), 500
156
 
 
62
  # response = processor.decode(outputs[0], skip_special_tokens=True)
63
  # return response
64
 
65
+
66
+ def predict_image(image_url, text):
67
+ try:
68
+ # Download the image from the URL
69
+ response = requests.get(image_url)
70
+ response.raise_for_status() # Raise an error for invalid responses
71
+ image = Image.open(io.BytesIO(response.content)).convert("RGB")
72
+
73
+ # Prepare the input messages
74
+ messages = [
75
+ {"role": "user", "content": [
76
+ {"type": "image"}, # Specify that an image is provided
77
+ {"type": "text", "text": text} # Add the user-provided text input
78
+ ]}
79
+ ]
80
+
81
+ # Create the input text using the processor's chat template
82
+ input_text = processor.apply_chat_template(messages, add_generation_prompt=True)
83
+
84
+ # Process the inputs and move to the appropriate device
85
+ inputs = processor(image=image, text=input_text, return_tensors="pt").to("cuda")
86
+
87
+ # Generate a response from the model
88
+ # outputs = model.generate(**inputs, max_new_tokens=100)
89
+
90
+ # # Decode the output to return the final response
91
+ # response = processor.decode(outputs[0], skip_special_tokens=True)
92
+
93
+ streamer = TextIteratorStreamer(processor, skip_special_tokens=True, skip_prompt=True)
94
+
95
+ generation_kwargs = dict(inputs, streamer=streamer, max_new_tokens=2048)
96
+ generated_text = ""
97
+
98
+ thread = Thread(target=model.generate, kwargs=generation_kwargs)
99
+ thread.start()
100
+ buffer = ""
101
+
102
+ for new_text in streamer:
103
+ buffer += new_text
104
+ # generated_text_without_prompt = buffer
105
+ # # time.sleep(0.01)
106
+ # yield buffer
107
+
108
+ return buffer
109
+ # return response
110
+
111
+ except Exception as e:
112
+ raise ValueError(f"Error during prediction: {str(e)}")
113
+
114
+
115
  def extract_text_from_pdf(pdf_url):
116
  try:
117
  response = requests.get(pdf_url)
 
178
  PROMPT_SKILLS = (
179
  "Extract the Course name and Primary Skills from this text. "
180
  )
181
+
182
+ PROMPT_IMAGE = (
183
+ "Extract the Student Name and transferred credits from this image "
184
+ )
185
+
186
  @app.route("/", methods=["GET"])
187
  def home():
188
  return jsonify({"message": "Welcome to the PDF Extraction API. Use the /extract endpoint to extract information."})
 
202
  pdf_text = extract_text_from_pdf(pdf_url)
203
  prompt = f"{PROMPT}\n\n{pdf_text}"
204
  response = predict_text(prompt)
205
+
206
  if data["skills"] == True:
207
  prompt_skills = f"{PROMPT_SKILLS}\n\n{pdf_text}"
208
  response_skills = predict_text(prompt_skills)
209
+
210
+ if data["img_url"] is not None:
211
+ prompt_skills = f"{PROMPT_IMAGE}\n"
212
+ response_image = predict_text(prompt_skills)
213
+ else
214
+ response_image = ''
215
+
216
+ return jsonify({"extracted_info": response + response_skills + response_image})
217
  except Exception as e:
218
  return jsonify({"error": str(e)}), 500
219