Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -14,33 +14,55 @@ client = InferenceClient(
|
|
| 14 |
|
| 15 |
app = Flask(__name__)
|
| 16 |
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
-
|
| 31 |
-
return f"Error generating blog: {str(e)}"
|
| 32 |
|
| 33 |
|
| 34 |
@app.route("/", methods=["GET", "POST"])
|
| 35 |
def index():
|
| 36 |
paragraph = ""
|
| 37 |
-
|
| 38 |
if request.method == "POST":
|
| 39 |
-
topic
|
|
|
|
|
|
|
| 40 |
if topic:
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
|
| 46 |
if __name__ == "__main__":
|
|
|
|
| 14 |
|
| 15 |
app = Flask(__name__)
|
| 16 |
|
| 17 |
+
# Models to try in order (first working one wins)
|
| 18 |
+
MODELS = [
|
| 19 |
+
"mistralai/Mistral-7B-Instruct-v0.3",
|
| 20 |
+
"HuggingFaceH4/zephyr-7b-beta",
|
| 21 |
+
"google/flan-t5-large",
|
| 22 |
+
]
|
| 23 |
|
| 24 |
+
def generate_blog(topic, mode="creative", language="English"):
|
| 25 |
+
tone = "creative and engaging" if mode == "creative" else "professional and formal"
|
| 26 |
+
prompt = (
|
| 27 |
+
f"Write a well-structured, {tone} blog paragraph in {language} "
|
| 28 |
+
f"about: {topic}\n\nBlog:"
|
| 29 |
+
)
|
| 30 |
|
| 31 |
+
for model in MODELS:
|
| 32 |
+
try:
|
| 33 |
+
response = client.text_generation(
|
| 34 |
+
model=model,
|
| 35 |
+
prompt=prompt,
|
| 36 |
+
max_new_tokens=300,
|
| 37 |
+
temperature=0.7,
|
| 38 |
+
)
|
| 39 |
+
return response.strip()
|
| 40 |
+
except Exception as e:
|
| 41 |
+
err = str(e)
|
| 42 |
+
# Only fall through on "not supported" / quota errors
|
| 43 |
+
if "not supported" in err or "quota" in err.lower() or "rate" in err.lower():
|
| 44 |
+
continue
|
| 45 |
+
# Any other error — return immediately with message
|
| 46 |
+
return f"Error generating blog: {err}"
|
| 47 |
|
| 48 |
+
return "Error: All models are currently unavailable. Please try again later."
|
|
|
|
| 49 |
|
| 50 |
|
| 51 |
@app.route("/", methods=["GET", "POST"])
|
| 52 |
def index():
|
| 53 |
paragraph = ""
|
| 54 |
+
error = ""
|
| 55 |
if request.method == "POST":
|
| 56 |
+
topic = request.form.get("topic", "").strip()
|
| 57 |
+
mode = request.form.get("mode", "creative")
|
| 58 |
+
language = request.form.get("language", "English")
|
| 59 |
if topic:
|
| 60 |
+
result = generate_blog(topic, mode, language)
|
| 61 |
+
if result.startswith("Error"):
|
| 62 |
+
error = result
|
| 63 |
+
else:
|
| 64 |
+
paragraph = result
|
| 65 |
+
return render_template("index.html", paragraph=paragraph, error=error)
|
| 66 |
|
| 67 |
|
| 68 |
if __name__ == "__main__":
|