File size: 3,857 Bytes
d5e59b9
d061bf7
771138c
 
 
d5e59b9
d061bf7
771138c
d5e59b9
 
 
771138c
 
 
d061bf7
d5e59b9
771138c
 
 
 
 
 
 
d061bf7
771138c
 
d061bf7
771138c
d061bf7
 
771138c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d061bf7
771138c
 
 
 
 
 
 
 
 
 
 
 
d5e59b9
771138c
 
 
 
d5e59b9
771138c
d5e59b9
771138c
 
d5e59b9
771138c
 
 
 
 
d5e59b9
771138c
 
 
 
 
 
d5e59b9
771138c
 
d061bf7
771138c
d061bf7
771138c
 
d061bf7
771138c
d061bf7
 
771138c
 
 
 
 
d061bf7
771138c
 
d061bf7
771138c
 
 
d5e59b9
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import os
import json
from flask import Flask, render_template, request, jsonify, redirect, url_for
from werkzeug.utils import secure_filename
from huggingface_hub import InferenceClient
import pandas as pd
import docx
from PyPDF2 import PdfReader

app = Flask(__name__)

# Set up file upload configurations
UPLOAD_FOLDER = "uploads"
app.config["UPLOAD_FOLDER"] = UPLOAD_FOLDER
ALLOWED_EXTENSIONS = {"txt", "csv", "json", "pdf", "docx"}

# Retrieve Hugging Face API key securely from environment variables
api_key = os.getenv("HF_API_KEY")
if not api_key:
    raise ValueError("Hugging Face API key not found. Set 'HF_API_KEY' in your Space secrets.")

# Initialize Hugging Face Inference Client
client = InferenceClient(api_key=api_key)


# Function to check allowed file types
def allowed_file(filename):
    return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS


# Function to read uploaded files and extract content
def extract_file_content(filepath, file_type):
    content = ""
    try:
        if file_type == "txt":
            with open(filepath, "r", encoding="utf-8") as file:
                content = file.read()
        elif file_type == "csv":
            df = pd.read_csv(filepath)
            content = df.to_string()
        elif file_type == "json":
            with open(filepath, "r", encoding="utf-8") as file:
                content = json.dumps(json.load(file), indent=4)
        elif file_type == "pdf":
            reader = PdfReader(filepath)
            content = "".join(page.extract_text() for page in reader.pages)
        elif file_type == "docx":
            doc = docx.Document(filepath)
            content = "\n".join(paragraph.text for paragraph in doc.paragraphs)
    except Exception as e:
        raise ValueError(f"Error extracting file content: {e}")
    return content


# Function to send content to Hugging Face model
def get_bot_response(prompt):
    try:
        response = client.text_generation(
            prompt=prompt,
            model="Qwen/Qwen2.5-Coder-32B-Instruct",
            max_tokens=500
        )
        return response
    except Exception as e:
        return f"Error in model response: {e}"


# Route: Home Page (File Upload Form)
@app.route("/", methods=["GET", "POST"])
def upload_file():
    if request.method == "POST":
        # Check if file is uploaded
        if "file" not in request.files:
            return jsonify({"error": "No file part"}), 400

        file = request.files["file"]

        if file.filename == "":
            return jsonify({"error": "No selected file"}), 400

        if file and allowed_file(file.filename):
            filename = secure_filename(file.filename)
            filepath = os.path.join(app.config["UPLOAD_FOLDER"], filename)
            os.makedirs(app.config["UPLOAD_FOLDER"], exist_ok=True)
            file.save(filepath)

            # Extract file content
            file_type = filename.rsplit(".", 1)[1].lower()
            try:
                content = extract_file_content(filepath, file_type)
            except Exception as e:
                return jsonify({"error": str(e)}), 500

            # Send content to Hugging Face model
            response = get_bot_response(content)

            return jsonify({"response": response})

        else:
            return jsonify({"error": "File type not allowed"}), 400

    return render_template("upload.html")


# Route: Retrieve Model Response (API Endpoint)
@app.route("/generate", methods=["POST"])
def generate_response():
    data = request.get_json()
    prompt = data.get("prompt")

    if not prompt:
        return jsonify({"error": "No prompt provided"}), 400

    # Send prompt to Hugging Face model
    response = get_bot_response(prompt)
    return jsonify({"response": response})


if __name__ == "__main__":
    app.run(debug=True)