Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,86 +1,66 @@
|
|
| 1 |
from flask import Flask, request, jsonify
|
| 2 |
-
from
|
| 3 |
-
import torch
|
| 4 |
import logging
|
| 5 |
-
|
| 6 |
|
| 7 |
app = Flask(__name__)
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
# Initialize model (will load on first request)
|
| 11 |
-
model_name = "google/gemma-1b-it"
|
| 12 |
-
tokenizer = None
|
| 13 |
-
model = None
|
| 14 |
-
generator = None
|
| 15 |
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 20 |
-
model = AutoModelForCausalLM.from_pretrained(
|
| 21 |
-
model_name,
|
| 22 |
-
torch_dtype=torch.float16,
|
| 23 |
-
device_map="auto"
|
| 24 |
-
)
|
| 25 |
-
generator = pipeline(
|
| 26 |
-
"text-generation",
|
| 27 |
-
model=model,
|
| 28 |
-
tokenizer=tokenizer,
|
| 29 |
-
device=0 if torch.cuda.is_available() else -1
|
| 30 |
-
)
|
| 31 |
|
| 32 |
-
@app.route('/generate_script', methods=['POST'])
|
| 33 |
def generate_script():
|
| 34 |
try:
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
data = request.json
|
|
|
|
|
|
|
| 38 |
topics = data.get('topics', [])
|
| 39 |
|
| 40 |
if not topics:
|
| 41 |
return jsonify({"error": "No topics provided"}), 400
|
| 42 |
|
| 43 |
-
#
|
| 44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
-
|
| 47 |
-
-
|
| 48 |
-
-
|
| 49 |
-
- Virtual descriptions for each scene
|
| 50 |
-
- Voiceover instructions for each scene
|
| 51 |
-
- Make it engaging and viral-worthy
|
| 52 |
|
| 53 |
-
|
|
|
|
|
|
|
| 54 |
"""
|
| 55 |
-
|
| 56 |
-
# Generate script
|
| 57 |
-
result = generator(
|
| 58 |
-
prompt,
|
| 59 |
-
max_length=1024,
|
| 60 |
-
temperature=0.8,
|
| 61 |
-
do_sample=True,
|
| 62 |
-
top_p=0.9,
|
| 63 |
-
num_return_sequences=1
|
| 64 |
-
)
|
| 65 |
-
|
| 66 |
-
generated_text = result[0]['generated_text']
|
| 67 |
-
|
| 68 |
-
# Extract just the script part (remove the prompt)
|
| 69 |
-
script = generated_text.replace(prompt, "").strip()
|
| 70 |
-
|
| 71 |
return jsonify({
|
| 72 |
"topic": topics[0],
|
| 73 |
-
"script":
|
| 74 |
-
"full_topics": topics
|
|
|
|
| 75 |
})
|
| 76 |
|
| 77 |
except Exception as e:
|
| 78 |
-
|
| 79 |
return jsonify({"error": str(e)}), 500
|
| 80 |
|
| 81 |
@app.route('/health', methods=['GET'])
|
| 82 |
def health():
|
| 83 |
-
return jsonify({"status": "healthy"})
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
|
| 85 |
if __name__ == '__main__':
|
| 86 |
-
|
|
|
|
|
|
| 1 |
from flask import Flask, request, jsonify
|
| 2 |
+
from flask_cors import CORS # Add this import
|
|
|
|
| 3 |
import logging
|
| 4 |
+
import os
|
| 5 |
|
| 6 |
app = Flask(__name__)
|
| 7 |
+
CORS(app) # Add this to handle CORS issues
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
+
# Configure logging
|
| 10 |
+
logging.basicConfig(level=logging.INFO)
|
| 11 |
+
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
+
@app.route('/generate_script', methods=['POST', 'GET']) # Allow GET for testing
|
| 14 |
def generate_script():
|
| 15 |
try:
|
| 16 |
+
logger.info("Received request to /generate_script")
|
| 17 |
+
|
| 18 |
+
if request.method == 'GET':
|
| 19 |
+
return jsonify({"message": "Use POST method with {'topics': ['topic1', 'topic2']}"})
|
| 20 |
|
| 21 |
data = request.json
|
| 22 |
+
logger.info(f"Request data: {data}")
|
| 23 |
+
|
| 24 |
topics = data.get('topics', [])
|
| 25 |
|
| 26 |
if not topics:
|
| 27 |
return jsonify({"error": "No topics provided"}), 400
|
| 28 |
|
| 29 |
+
# For now, return a mock response to test the connection
|
| 30 |
+
mock_script = f"""VIRAL SCRIPT FOR: {topics[0]}
|
| 31 |
+
|
| 32 |
+
Scene 1: Golden Hook
|
| 33 |
+
- Visual: Eye-catching intro with text overlay "THIS WILL CHANGE EVERYTHING"
|
| 34 |
+
- Voiceover: "What if I told you {topics[0].lower()} is about to revolutionize everything?"
|
| 35 |
|
| 36 |
+
Scene 2: Valuable Content
|
| 37 |
+
- Visual: Demonstrating key points with graphics
|
| 38 |
+
- Voiceover: "Here are 3 ways {topics[0].lower()} is changing the game..."
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
+
Scene 3: CTA
|
| 41 |
+
- Visual: Call-to-action screen with links
|
| 42 |
+
- Voiceover: "Like and follow for more insights!"
|
| 43 |
"""
|
| 44 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
return jsonify({
|
| 46 |
"topic": topics[0],
|
| 47 |
+
"script": mock_script,
|
| 48 |
+
"full_topics": topics,
|
| 49 |
+
"status": "success"
|
| 50 |
})
|
| 51 |
|
| 52 |
except Exception as e:
|
| 53 |
+
logger.error(f"Error: {str(e)}")
|
| 54 |
return jsonify({"error": str(e)}), 500
|
| 55 |
|
| 56 |
@app.route('/health', methods=['GET'])
|
| 57 |
def health():
|
| 58 |
+
return jsonify({"status": "healthy", "message": "Server is running"})
|
| 59 |
+
|
| 60 |
+
@app.route('/test', methods=['GET'])
|
| 61 |
+
def test():
|
| 62 |
+
return jsonify({"message": "Test endpoint working!"})
|
| 63 |
|
| 64 |
if __name__ == '__main__':
|
| 65 |
+
port = int(os.environ.get('PORT', 7860))
|
| 66 |
+
app.run(host='0.0.0.0', port=port, debug=False)
|