Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files- app.py +74 -0
- requirements.txt +4 -0
app.py
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from flask import Flask, request, jsonify
|
| 2 |
+
import torch
|
| 3 |
+
from transformers import pipeline
|
| 4 |
+
import os
|
| 5 |
+
|
| 6 |
+
# Model ka naam
|
| 7 |
+
model_name = "mistralai/Mistral-7B-Instruct-v0.2"
|
| 8 |
+
|
| 9 |
+
print("Attempting to load model...")
|
| 10 |
+
try:
|
| 11 |
+
# AI Pipeline ko load karna
|
| 12 |
+
pipe = pipeline(
|
| 13 |
+
"text-generation",
|
| 14 |
+
model=model_name,
|
| 15 |
+
torch_dtype=torch.bfloat16,
|
| 16 |
+
device_map="auto"
|
| 17 |
+
)
|
| 18 |
+
print("✅ Model loaded successfully!")
|
| 19 |
+
except Exception as e:
|
| 20 |
+
print(f"❌ Error loading model: {e}")
|
| 21 |
+
pipe = None
|
| 22 |
+
|
| 23 |
+
app = Flask(__name__)
|
| 24 |
+
|
| 25 |
+
# Meta-prompt ka template
|
| 26 |
+
meta_prompt_template = """
|
| 27 |
+
You are a creative food writer and an expert chef. Based on the following cooking step, you must generate a JSON object with two keys: "tip" and "description".
|
| 28 |
+
|
| 29 |
+
1. For the "tip" key, provide a single, short, and engaging Pro-Tip or a Fun Fact related to an ingredient or technique in the step.
|
| 30 |
+
2. For the "description" key, write a creative, deep, and evocative description of the cooking action, as if you are narrating a cinematic cooking show. Use sensory language (smell, sound, sight).
|
| 31 |
+
|
| 32 |
+
You must not output any text other than the final, clean JSON object.
|
| 33 |
+
|
| 34 |
+
Current Cooking Step: "{step_text}"
|
| 35 |
+
"""
|
| 36 |
+
|
| 37 |
+
@app.route('/generate-content', methods=['POST'])
|
| 38 |
+
def generate_content():
|
| 39 |
+
if not pipe:
|
| 40 |
+
return jsonify({"error": "Model is not available or failed to load"}), 503
|
| 41 |
+
|
| 42 |
+
data = request.get_json()
|
| 43 |
+
if not data or 'step_text' not in data:
|
| 44 |
+
return jsonify({"error": "step_text is missing from the request body"}), 400
|
| 45 |
+
|
| 46 |
+
step_text = data.get('step_text')
|
| 47 |
+
|
| 48 |
+
try:
|
| 49 |
+
final_prompt = meta_prompt_template.format(step_text=step_text)
|
| 50 |
+
|
| 51 |
+
outputs = pipe(final_prompt, max_new_tokens=256, do_sample=True, temperature=0.7, top_k=50, top_p=0.95)
|
| 52 |
+
|
| 53 |
+
generated_text = outputs[0]['generated_text']
|
| 54 |
+
|
| 55 |
+
# Model ke output se saaf JSON nikalna
|
| 56 |
+
json_part = generated_text[generated_text.find('{'):generated_text.rfind('}')+1]
|
| 57 |
+
|
| 58 |
+
# String ko JSON mein convert karna
|
| 59 |
+
response_data = eval(json_part)
|
| 60 |
+
|
| 61 |
+
return jsonify(response_data)
|
| 62 |
+
|
| 63 |
+
except Exception as e:
|
| 64 |
+
print(f"Error during generation: {e}")
|
| 65 |
+
return jsonify({"error": str(e)}), 500
|
| 66 |
+
|
| 67 |
+
@app.route('/', methods=['GET'])
|
| 68 |
+
def home():
|
| 69 |
+
return "Creative Writer API is running!", 200
|
| 70 |
+
|
| 71 |
+
# Hugging Face Spaces ke liye
|
| 72 |
+
if __name__ == '__main__':
|
| 73 |
+
port = int(os.environ.get("PORT", 7860))
|
| 74 |
+
app.run(host='0.0.0.0', port=port)
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
flask
|
| 2 |
+
transformers
|
| 3 |
+
torch
|
| 4 |
+
accelerate
|