Spaces:
Sleeping

Gigishot commited on
Commit
1650d46
·
verified ·
1 Parent(s): 1c1f386

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -17
app.py CHANGED
@@ -14,33 +14,55 @@ client = InferenceClient(
14
 
15
  app = Flask(__name__)
16
 
17
- def generate_blog(topic):
18
- try:
19
- prompt = f"Write a well-structured blog paragraph about: {topic}"
 
 
 
20
 
21
- response = client.text_generation(
22
- model="tiiuae/falcon-7b-instruct", # ✅ supported model
23
- prompt=prompt,
24
- max_new_tokens=300,
25
- temperature=0.7,
26
- )
27
 
28
- return response
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29
 
30
- except Exception as e:
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 = request.form.get("topic")
 
 
40
  if topic:
41
- paragraph = generate_blog(topic)
42
-
43
- return render_template("index.html", paragraph=paragraph)
 
 
 
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__":