Spaces:
Running
Running
GabrielSalem
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -1,145 +1,120 @@
|
|
1 |
-
from flask import Flask, render_template, request, jsonify, redirect, url_for
|
2 |
-
from huggingface_hub import InferenceClient
|
3 |
import os
|
4 |
import json
|
|
|
|
|
|
|
5 |
import pandas as pd
|
6 |
-
import PyPDF2
|
7 |
import docx
|
8 |
-
from
|
9 |
|
10 |
app = Flask(__name__)
|
11 |
-
app.config["UPLOAD_FOLDER"] = "uploads"
|
12 |
-
app.config["HISTORY_FILE"] = "history.json"
|
13 |
|
14 |
-
#
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
# Allowed file extensions
|
19 |
ALLOWED_EXTENSIONS = {"txt", "csv", "json", "pdf", "docx"}
|
20 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
|
22 |
-
|
|
|
23 |
def allowed_file(filename):
|
24 |
-
return
|
25 |
|
26 |
|
27 |
-
#
|
28 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
try:
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
# Utility: Extract text from files
|
43 |
-
def extract_text(file_path, file_type):
|
44 |
-
if file_type == "txt":
|
45 |
-
with open(file_path, "r") as f:
|
46 |
-
return f.read()
|
47 |
-
elif file_type == "csv":
|
48 |
-
df = pd.read_csv(file_path)
|
49 |
-
return df.to_string()
|
50 |
-
elif file_type == "json":
|
51 |
-
with open(file_path, "r") as f:
|
52 |
-
data = json.load(f)
|
53 |
-
return json.dumps(data, indent=4)
|
54 |
-
elif file_type == "pdf":
|
55 |
-
text = ""
|
56 |
-
with open(file_path, "rb") as f:
|
57 |
-
reader = PyPDF2.PdfReader(f)
|
58 |
-
for page in reader.pages:
|
59 |
-
text += page.extract_text()
|
60 |
-
return text
|
61 |
-
elif file_type == "docx":
|
62 |
-
doc = docx.Document(file_path)
|
63 |
-
return "\n".join([p.text for p in doc.paragraphs])
|
64 |
-
else:
|
65 |
-
return ""
|
66 |
-
|
67 |
-
|
68 |
-
# Hugging Face Chat Response
|
69 |
-
def get_bot_response(messages):
|
70 |
-
stream = client.chat.completions.create(
|
71 |
-
model="Qwen/Qwen2.5-Coder-32B-Instruct",
|
72 |
-
messages=messages,
|
73 |
-
max_tokens=500,
|
74 |
-
stream=True
|
75 |
-
)
|
76 |
-
bot_response = ""
|
77 |
-
for chunk in stream:
|
78 |
-
if chunk.choices and len(chunk.choices) > 0:
|
79 |
-
new_content = chunk.choices[0].delta.content
|
80 |
-
bot_response += new_content
|
81 |
-
return bot_response
|
82 |
-
|
83 |
-
|
84 |
-
@app.route("/")
|
85 |
-
def home():
|
86 |
-
history = load_history()
|
87 |
-
return render_template("home.html", history=history)
|
88 |
-
|
89 |
-
|
90 |
-
@app.route("/upload", methods=["POST"])
|
91 |
def upload_file():
|
92 |
-
if "
|
93 |
-
|
|
|
|
|
94 |
|
95 |
-
|
96 |
-
if file and allowed_file(file.filename):
|
97 |
-
filename = secure_filename(file.filename)
|
98 |
-
file_path = os.path.join(app.config["UPLOAD_FOLDER"], filename)
|
99 |
-
os.makedirs(app.config["UPLOAD_FOLDER"], exist_ok=True)
|
100 |
-
file.save(file_path)
|
101 |
|
102 |
-
|
103 |
-
|
104 |
-
extracted_text = extract_text(file_path, file_type)
|
105 |
|
106 |
-
|
107 |
-
|
108 |
-
|
|
|
|
|
109 |
|
110 |
-
|
111 |
-
|
112 |
-
|
|
|
|
|
|
|
113 |
|
114 |
-
|
|
|
115 |
|
116 |
-
|
117 |
-
else:
|
118 |
-
return jsonify({"error": "Invalid file type"}), 400
|
119 |
|
|
|
|
|
120 |
|
121 |
-
|
122 |
-
def generate_response():
|
123 |
-
data = request.json
|
124 |
-
user_message = data.get("message")
|
125 |
-
if not user_message:
|
126 |
-
return jsonify({"error": "Message is required"}), 400
|
127 |
|
128 |
-
# Update conversation history
|
129 |
-
history = load_history()
|
130 |
-
history.append({"role": "user", "content": user_message})
|
131 |
|
132 |
-
|
133 |
-
|
134 |
-
|
|
|
|
|
135 |
|
136 |
-
|
|
|
137 |
|
138 |
-
|
|
|
|
|
139 |
|
140 |
|
141 |
if __name__ == "__main__":
|
142 |
-
os.makedirs(app.config["UPLOAD_FOLDER"], exist_ok=True)
|
143 |
app.run(debug=True)
|
144 |
-
|
145 |
-
|
|
|
|
|
|
|
1 |
import os
|
2 |
import json
|
3 |
+
from flask import Flask, render_template, request, jsonify, redirect, url_for
|
4 |
+
from werkzeug.utils import secure_filename
|
5 |
+
from huggingface_hub import InferenceClient
|
6 |
import pandas as pd
|
|
|
7 |
import docx
|
8 |
+
from PyPDF2 import PdfReader
|
9 |
|
10 |
app = Flask(__name__)
|
|
|
|
|
11 |
|
12 |
+
# Set up file upload configurations
|
13 |
+
UPLOAD_FOLDER = "uploads"
|
14 |
+
app.config["UPLOAD_FOLDER"] = UPLOAD_FOLDER
|
|
|
|
|
15 |
ALLOWED_EXTENSIONS = {"txt", "csv", "json", "pdf", "docx"}
|
16 |
|
17 |
+
# Retrieve Hugging Face API key securely from environment variables
|
18 |
+
api_key = os.getenv("HF_API_KEY")
|
19 |
+
if not api_key:
|
20 |
+
raise ValueError("Hugging Face API key not found. Set 'HF_API_KEY' in your Space secrets.")
|
21 |
+
|
22 |
+
# Initialize Hugging Face Inference Client
|
23 |
+
client = InferenceClient(api_key=api_key)
|
24 |
|
25 |
+
|
26 |
+
# Function to check allowed file types
|
27 |
def allowed_file(filename):
|
28 |
+
return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
|
29 |
|
30 |
|
31 |
+
# Function to read uploaded files and extract content
|
32 |
+
def extract_file_content(filepath, file_type):
|
33 |
+
content = ""
|
34 |
+
try:
|
35 |
+
if file_type == "txt":
|
36 |
+
with open(filepath, "r", encoding="utf-8") as file:
|
37 |
+
content = file.read()
|
38 |
+
elif file_type == "csv":
|
39 |
+
df = pd.read_csv(filepath)
|
40 |
+
content = df.to_string()
|
41 |
+
elif file_type == "json":
|
42 |
+
with open(filepath, "r", encoding="utf-8") as file:
|
43 |
+
content = json.dumps(json.load(file), indent=4)
|
44 |
+
elif file_type == "pdf":
|
45 |
+
reader = PdfReader(filepath)
|
46 |
+
content = "".join(page.extract_text() for page in reader.pages)
|
47 |
+
elif file_type == "docx":
|
48 |
+
doc = docx.Document(filepath)
|
49 |
+
content = "\n".join(paragraph.text for paragraph in doc.paragraphs)
|
50 |
+
except Exception as e:
|
51 |
+
raise ValueError(f"Error extracting file content: {e}")
|
52 |
+
return content
|
53 |
+
|
54 |
+
|
55 |
+
# Function to send content to Hugging Face model
|
56 |
+
def get_bot_response(prompt):
|
57 |
try:
|
58 |
+
response = client.text_generation(
|
59 |
+
prompt=prompt,
|
60 |
+
model="Qwen/Qwen2.5-Coder-32B-Instruct",
|
61 |
+
max_tokens=500
|
62 |
+
)
|
63 |
+
return response
|
64 |
+
except Exception as e:
|
65 |
+
return f"Error in model response: {e}"
|
66 |
+
|
67 |
+
|
68 |
+
# Route: Home Page (File Upload Form)
|
69 |
+
@app.route("/", methods=["GET", "POST"])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
70 |
def upload_file():
|
71 |
+
if request.method == "POST":
|
72 |
+
# Check if file is uploaded
|
73 |
+
if "file" not in request.files:
|
74 |
+
return jsonify({"error": "No file part"}), 400
|
75 |
|
76 |
+
file = request.files["file"]
|
|
|
|
|
|
|
|
|
|
|
77 |
|
78 |
+
if file.filename == "":
|
79 |
+
return jsonify({"error": "No selected file"}), 400
|
|
|
80 |
|
81 |
+
if file and allowed_file(file.filename):
|
82 |
+
filename = secure_filename(file.filename)
|
83 |
+
filepath = os.path.join(app.config["UPLOAD_FOLDER"], filename)
|
84 |
+
os.makedirs(app.config["UPLOAD_FOLDER"], exist_ok=True)
|
85 |
+
file.save(filepath)
|
86 |
|
87 |
+
# Extract file content
|
88 |
+
file_type = filename.rsplit(".", 1)[1].lower()
|
89 |
+
try:
|
90 |
+
content = extract_file_content(filepath, file_type)
|
91 |
+
except Exception as e:
|
92 |
+
return jsonify({"error": str(e)}), 500
|
93 |
|
94 |
+
# Send content to Hugging Face model
|
95 |
+
response = get_bot_response(content)
|
96 |
|
97 |
+
return jsonify({"response": response})
|
|
|
|
|
98 |
|
99 |
+
else:
|
100 |
+
return jsonify({"error": "File type not allowed"}), 400
|
101 |
|
102 |
+
return render_template("upload.html")
|
|
|
|
|
|
|
|
|
|
|
103 |
|
|
|
|
|
|
|
104 |
|
105 |
+
# Route: Retrieve Model Response (API Endpoint)
|
106 |
+
@app.route("/generate", methods=["POST"])
|
107 |
+
def generate_response():
|
108 |
+
data = request.get_json()
|
109 |
+
prompt = data.get("prompt")
|
110 |
|
111 |
+
if not prompt:
|
112 |
+
return jsonify({"error": "No prompt provided"}), 400
|
113 |
|
114 |
+
# Send prompt to Hugging Face model
|
115 |
+
response = get_bot_response(prompt)
|
116 |
+
return jsonify({"response": response})
|
117 |
|
118 |
|
119 |
if __name__ == "__main__":
|
|
|
120 |
app.run(debug=True)
|
|
|
|