File size: 3,026 Bytes
5ac52e2
 
 
 
2770a31
5ac52e2
 
2905401
5ac52e2
 
 
 
 
 
 
2770a31
 
5ac52e2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2770a31
5ac52e2
 
 
 
2770a31
5ac52e2
2770a31
5ac52e2
2770a31
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5ac52e2
 
 
 
 
 
 
 
 
 
 
 
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
from flask import Flask, request, jsonify
import torch
from transformers import pipeline
import os
import json # <-- Humne yeh nayi library import ki hai

# Model ka naam
model_name = "TinyLlama/TinyLlama-1.1B-Chat-v1.0"

print("Attempting to load model...")
try:
    # AI Pipeline ko load karna
    pipe = pipeline(
        "text-generation",
        model=model_name,
        torch_dtype=torch.float32, # CPU ke liye float32 behtar hai
        device=-1 # CPU par run karne ke liye force karna
    )
    print("✅ Model loaded successfully!")
except Exception as e:
    print(f"❌ Error loading model: {e}")
    pipe = None

app = Flask(__name__)

# Meta-prompt ka template
meta_prompt_template = """
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".

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.
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).

You must not output any text other than the final, clean JSON object.

Current Cooking Step: "{step_text}"
"""

@app.route('/generate-content', methods=['POST'])
def generate_content():
    if not pipe:
        return jsonify({"error": "Model is not available or failed to load"}), 503

    data = request.get_json()
    if not data or 'step_text' not in data:
        return jsonify({"error": "step_text is missing from the request body"}), 400
    
    step_text = data.get('step_text')

    try:
        final_prompt = meta_prompt_template.format(step_text=step_text)
        
        outputs = pipe(final_prompt, max_new_tokens=256, do_sample=True, temperature=0.7, top_k=50, top_p=0.95)
        
        generated_text = outputs[0]['generated_text']
        
        # Naya, behtar tareeka JSON nikalne ka
        start_index = generated_text.find('{')
        end_index = generated_text.rfind('}') + 1
        
        if start_index != -1 and end_index != -1:
            json_part = generated_text[start_index:end_index]
            # String ko JSON mein convert karna
            response_data = json.loads(json_part)
            return jsonify(response_data)
        else:
            return jsonify({"error": "Failed to find a valid JSON object in the model's response.", "raw_response": generated_text}), 500

    except json.JSONDecodeError:
        return jsonify({"error": "Model returned a malformed JSON string.", "raw_response": generated_text}), 500
    except Exception as e:
        print(f"Error during generation: {e}")
        return jsonify({"error": str(e)}), 500

@app.route('/', methods=['GET'])
def home():
    return "Creative Writer API is running!", 200

# Hugging Face Spaces ke liye
if __name__ == '__main__':
    port = int(os.environ.get("PORT", 7860))
    app.run(host='0.0.0.0', port=port)