user commited on
Commit
6e90287
·
1 Parent(s): 219d24c

update. focus on remote and pdf processing

Browse files
Files changed (1) hide show
  1. app.py +42 -75
app.py CHANGED
@@ -2,92 +2,58 @@ from flask import Flask, request, jsonify, send_from_directory
2
  import requests
3
  import os
4
  from dotenv import load_dotenv
5
- import traceback
6
- from PIL import Image
7
- from pdf2image import convert_from_bytes
8
- import base64
9
- from io import BytesIO
10
 
11
  load_dotenv()
12
 
13
  app = Flask(__name__)
14
 
 
 
 
15
  API_URL = "https://api-inference.huggingface.co/models/"
16
  headers = {"Authorization": f"Bearer {os.getenv('HUGGINGFACE_API_KEY')}"}
17
- HF_REPO_ID = os.getenv('HF_REPO_ID') # Your Hugging Face repo ID
 
18
 
19
  def query(payload, model):
20
- response = requests.post(API_URL + model, headers=headers, json=payload)
21
- return response.json()
 
 
 
 
 
22
 
23
- # Process PDFs using Hugging Face's PDF processing model
24
- def process_pdfs():
25
- pdf_url = f"https://huggingface.co/spaces/{HF_REPO_ID}/resolve/main/data/your_pdf_file.pdf"
26
 
27
- try:
28
- # Download PDF
29
- pdf_response = requests.get(pdf_url)
30
- pdf_response.raise_for_status() # This will raise an exception for HTTP errors
31
-
32
- print(f"PDF downloaded successfully. Content length: {len(pdf_response.content)} bytes")
33
-
34
- # Convert PDF to images
35
- images = convert_from_bytes(pdf_response.content)
36
- print(f"Converted PDF to {len(images)} images")
37
-
38
- # Process each image
39
- vision_model = "google/vit-base-patch16-224"
40
- summaries = []
41
-
42
- for i, image in enumerate(images):
43
- # Convert image to base64
44
- buffered = BytesIO()
45
- image.save(buffered, format="PNG")
46
- img_str = base64.b64encode(buffered.getvalue()).decode()
47
-
48
- # Process image with vision model
49
- payload = {
50
- "inputs": [
51
- {
52
- "image": img_str,
53
- "text": "Describe the content of this image in detail."
54
- }
55
- ]
56
- }
57
- response = query(payload, vision_model)
58
- print(f"Page {i+1} processing response:", json.dumps(response, indent=2))
59
-
60
- if isinstance(response, list) and len(response) > 0 and 'generated_text' in response[0]:
61
- summaries.append(response[0]['generated_text'])
62
- else:
63
- summaries.append(f"Error processing page {i+1}")
64
-
65
- return " ".join(summaries)
66
- except Exception as e:
67
- print(f"Error in process_pdfs: {str(e)}")
68
- print(traceback.format_exc())
69
- return f"Error processing PDF: {str(e)}"
70
 
71
- # Get the summary of PDFs
72
- pdf_summary = process_pdfs()
73
- print("PDF Summary:", pdf_summary)
74
 
75
  # Get embeddings for the summary
76
  embedding_model = "sentence-transformers/all-MiniLM-L6-v2"
77
- if not pdf_summary.startswith("Error"):
78
- try:
79
- summary_embedding = query({"inputs": pdf_summary}, embedding_model)[0]
80
- print("Successfully created summary embedding")
81
- except Exception as e:
82
- print(f"Error getting embedding: {str(e)}")
83
- print(traceback.format_exc())
84
- summary_embedding = None
85
- else:
86
- print("Skipping embedding due to PDF processing error")
87
- summary_embedding = None
88
-
89
- if summary_embedding is None:
90
- print("WARNING: summary_embedding is None. The chatbot will not be able to provide meaningful responses.")
91
 
92
  @app.route('/')
93
  def home():
@@ -97,17 +63,18 @@ def home():
97
  def ask():
98
  prompt = request.json['question']
99
 
 
 
 
100
  # Get embedding for the question
101
  query_embedding = query({"inputs": prompt}, embedding_model)[0]
102
 
103
  # Calculate similarity
104
  similarity = sum(a*b for a, b in zip(query_embedding, summary_embedding))
105
 
106
- # Generate character response
107
- character_traits = "I am a knowledgeable and friendly AI assistant based on the content of the provided PDFs."
108
- input_text = f"Character: {character_traits}\nContext: {pdf_summary}\nUser: {prompt}\nCharacter's response:"
109
-
110
  generator_model = "google/flan-t5-base"
 
111
  response = query({"inputs": input_text}, generator_model)[0]["generated_text"]
112
 
113
  return jsonify({'response': response})
 
2
  import requests
3
  import os
4
  from dotenv import load_dotenv
5
+ import logging
 
 
 
 
6
 
7
  load_dotenv()
8
 
9
  app = Flask(__name__)
10
 
11
+ logging.basicConfig(level=logging.INFO)
12
+ logger = logging.getLogger(__name__)
13
+
14
  API_URL = "https://api-inference.huggingface.co/models/"
15
  headers = {"Authorization": f"Bearer {os.getenv('HUGGINGFACE_API_KEY')}"}
16
+ HF_REPO_ID = os.getenv('HF_REPO_ID')
17
+ PDF_FILENAME = os.getenv('PDF_FILENAME')
18
 
19
  def query(payload, model):
20
+ try:
21
+ response = requests.post(API_URL + model, headers=headers, json=payload)
22
+ response.raise_for_status()
23
+ return response.json()
24
+ except requests.exceptions.RequestException as e:
25
+ logger.error(f"Error querying model {model}: {str(e)}")
26
+ return {"error": str(e)}
27
 
28
+ def process_pdf():
29
+ pdf_url = f"https://huggingface.co/spaces/{HF_REPO_ID}/resolve/main/data/{PDF_FILENAME}"
30
+ logger.info(f"Attempting to process PDF at URL: {pdf_url}")
31
 
32
+ model = "impira/layoutlm-document-qa"
33
+ payload = {
34
+ "inputs": {
35
+ "question": "Summarize the main points of this document.",
36
+ "image": pdf_url
37
+ }
38
+ }
39
+
40
+ response = query(payload, model)
41
+ logger.info(f"PDF processing response: {response}")
42
+
43
+ if 'error' in response:
44
+ return f"Error processing PDF: {response['error']}"
45
+ elif 'answer' in response:
46
+ return response['answer']
47
+ else:
48
+ return str(response)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49
 
50
+ # Process PDF and get summary
51
+ pdf_summary = process_pdf()
52
+ logger.info(f"PDF Summary: {pdf_summary}")
53
 
54
  # Get embeddings for the summary
55
  embedding_model = "sentence-transformers/all-MiniLM-L6-v2"
56
+ summary_embedding = query({"inputs": pdf_summary}, embedding_model)[0] if not pdf_summary.startswith("Error") else None
 
 
 
 
 
 
 
 
 
 
 
 
 
57
 
58
  @app.route('/')
59
  def home():
 
63
  def ask():
64
  prompt = request.json['question']
65
 
66
+ if summary_embedding is None:
67
+ return jsonify({'response': "I'm sorry, but I couldn't process the PDF correctly. Please check the PDF file and try again later."})
68
+
69
  # Get embedding for the question
70
  query_embedding = query({"inputs": prompt}, embedding_model)[0]
71
 
72
  # Calculate similarity
73
  similarity = sum(a*b for a, b in zip(query_embedding, summary_embedding))
74
 
75
+ # Generate response using T5 model
 
 
 
76
  generator_model = "google/flan-t5-base"
77
+ input_text = f"Context: {pdf_summary}\n\nQuestion: {prompt}\n\nAnswer:"
78
  response = query({"inputs": input_text}, generator_model)[0]["generated_text"]
79
 
80
  return jsonify({'response': response})