Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from flask import Flask, request, jsonify
|
2 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
3 |
+
import torch
|
4 |
+
|
5 |
+
# Initialize the model and tokenizer
|
6 |
+
model = AutoModelForCausalLM.from_pretrained("anto18671/lumenspark", trust_remote_code=True)
|
7 |
+
tokenizer = AutoTokenizer.from_pretrained("anto18671/lumenspark", trust_remote_code=True)
|
8 |
+
|
9 |
+
# Set up Flask application
|
10 |
+
app = Flask(__name__)
|
11 |
+
|
12 |
+
# Define inference endpoint
|
13 |
+
@app.route("/generate", methods=["POST"])
|
14 |
+
def generate_text():
|
15 |
+
data = request.get_json()
|
16 |
+
|
17 |
+
# Extract the input text
|
18 |
+
text = data.get("text", "")
|
19 |
+
if not text:
|
20 |
+
return jsonify({"error": "Input text is required"}), 400
|
21 |
+
|
22 |
+
# Tokenize input text
|
23 |
+
encoded_input = tokenizer(text, return_tensors='pt')
|
24 |
+
|
25 |
+
# Generate text using the model
|
26 |
+
output = model.generate(
|
27 |
+
input_ids=encoded_input["input_ids"],
|
28 |
+
attention_mask=encoded_input["attention_mask"],
|
29 |
+
max_length=100,
|
30 |
+
min_length=20,
|
31 |
+
temperature=0.6,
|
32 |
+
top_k=50,
|
33 |
+
top_p=0.9,
|
34 |
+
repetition_penalty=1.1,
|
35 |
+
do_sample=True
|
36 |
+
)
|
37 |
+
|
38 |
+
# Decode the generated text
|
39 |
+
decoded_text = tokenizer.decode(output[0], skip_special_tokens=True)
|
40 |
+
|
41 |
+
# Return generated text as JSON response
|
42 |
+
return jsonify({"generated_text": decoded_text})
|
43 |
+
|
44 |
+
# Run the Flask app
|
45 |
+
if __name__ == "__main__":
|
46 |
+
app.run(host="0.0.0.0", port=5000)
|