Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,120 +1,79 @@
|
|
| 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
|
| 8 |
from PyPDF2 import PdfReader
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 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("APIHUGGING")
|
| 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=
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
try:
|
| 35 |
-
|
| 36 |
-
|
| 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 |
-
#
|
| 56 |
-
|
| 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 |
-
#
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
prompt = data.get("prompt")
|
| 110 |
|
| 111 |
-
|
| 112 |
-
|
|
|
|
| 113 |
|
| 114 |
-
|
| 115 |
-
response = get_bot_response(prompt)
|
| 116 |
-
return jsonify({"response": response})
|
| 117 |
|
|
|
|
|
|
|
| 118 |
|
|
|
|
| 119 |
if __name__ == "__main__":
|
| 120 |
-
app.
|
|
|
|
| 1 |
import os
|
| 2 |
import json
|
|
|
|
|
|
|
|
|
|
| 3 |
import pandas as pd
|
| 4 |
+
from docx import Document
|
| 5 |
from PyPDF2 import PdfReader
|
| 6 |
+
from huggingface_hub import InferenceClient
|
| 7 |
+
import gradio as gr
|
| 8 |
|
| 9 |
+
# Retrieve Hugging Face API key from environment variable (secret)
|
| 10 |
+
API_KEY = os.getenv("APIHUGGING")
|
| 11 |
+
if not API_KEY:
|
| 12 |
+
raise ValueError("Hugging Face API key not found. Please set the 'APIHUGGING' secret.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
# Initialize Hugging Face Inference Client
|
| 15 |
+
client = InferenceClient(api_key=API_KEY)
|
| 16 |
+
|
| 17 |
+
# Function to extract text from various file types
|
| 18 |
+
def extract_file_content(file_path):
|
| 19 |
+
_, file_extension = os.path.splitext(file_path.name)
|
| 20 |
+
if file_extension.lower() in [".txt"]:
|
| 21 |
+
return file_path.read().decode("utf-8")
|
| 22 |
+
elif file_extension.lower() in [".csv"]:
|
| 23 |
+
df = pd.read_csv(file_path)
|
| 24 |
+
return df.to_string(index=False)
|
| 25 |
+
elif file_extension.lower() in [".json"]:
|
| 26 |
+
data = json.load(file_path)
|
| 27 |
+
return json.dumps(data, indent=4)
|
| 28 |
+
elif file_extension.lower() in [".pdf"]:
|
| 29 |
+
reader = PdfReader(file_path)
|
| 30 |
+
text = ""
|
| 31 |
+
for page in reader.pages:
|
| 32 |
+
text += page.extract_text()
|
| 33 |
+
return text
|
| 34 |
+
elif file_extension.lower() in [".docx"]:
|
| 35 |
+
doc = Document(file_path)
|
| 36 |
+
return "\n".join([para.text for para in doc.paragraphs])
|
| 37 |
+
else:
|
| 38 |
+
return "Unsupported file type."
|
| 39 |
+
|
| 40 |
+
# Function to interact with the Hugging Face model
|
| 41 |
+
def get_bot_response(file, prompt):
|
| 42 |
try:
|
| 43 |
+
# Extract content from the uploaded file
|
| 44 |
+
file_content = extract_file_content(file)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
+
# Prepare conversation history
|
| 47 |
+
messages = [
|
| 48 |
+
{"role": "user", "content": f"{prompt}\n\nFile Content:\n{file_content}"}
|
| 49 |
+
]
|
| 50 |
|
| 51 |
+
# Call Hugging Face API
|
| 52 |
+
bot_response = client.chat_completions.create(
|
|
|
|
|
|
|
|
|
|
| 53 |
model="Qwen/Qwen2.5-Coder-32B-Instruct",
|
| 54 |
+
messages=messages,
|
| 55 |
max_tokens=500
|
| 56 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
|
| 58 |
+
# Collect and return the bot's response
|
| 59 |
+
return bot_response.choices[0].message.content
|
| 60 |
+
except Exception as e:
|
| 61 |
+
return f"Error: {str(e)}"
|
| 62 |
|
| 63 |
+
# Gradio Interface
|
| 64 |
+
with gr.Blocks() as app:
|
| 65 |
+
gr.Markdown("# π AI File Chat with Hugging Face π")
|
| 66 |
+
gr.Markdown("Upload any file and ask the AI a question based on the file's content!")
|
|
|
|
| 67 |
|
| 68 |
+
with gr.Row():
|
| 69 |
+
file_input = gr.File(label="Upload File")
|
| 70 |
+
prompt_input = gr.Textbox(label="Enter your question", placeholder="Ask something about the uploaded file...")
|
| 71 |
|
| 72 |
+
output = gr.Textbox(label="AI Response")
|
|
|
|
|
|
|
| 73 |
|
| 74 |
+
submit_button = gr.Button("Submit")
|
| 75 |
+
submit_button.click(get_bot_response, inputs=[file_input, prompt_input], outputs=output)
|
| 76 |
|
| 77 |
+
# Launch the Gradio app
|
| 78 |
if __name__ == "__main__":
|
| 79 |
+
app.launch()
|