Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,67 +3,66 @@ from dotenv import load_dotenv
|
|
| 3 |
from huggingface_hub import InferenceClient
|
| 4 |
import os
|
| 5 |
|
| 6 |
-
# Load env variables
|
| 7 |
load_dotenv()
|
| 8 |
|
| 9 |
-
|
|
|
|
| 10 |
client = InferenceClient(
|
| 11 |
-
provider="hf-inference",
|
| 12 |
api_key=os.getenv("HF_API_KEY")
|
| 13 |
)
|
| 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
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
|
|
|
| 30 |
|
| 31 |
for model in MODELS:
|
| 32 |
try:
|
| 33 |
response = client.text_generation(
|
| 34 |
model=model,
|
| 35 |
prompt=prompt,
|
| 36 |
-
max_new_tokens=
|
| 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 |
-
|
| 49 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
|
| 51 |
@app.route("/", methods=["GET", "POST"])
|
| 52 |
def index():
|
| 53 |
paragraph = ""
|
| 54 |
error = ""
|
|
|
|
| 55 |
if request.method == "POST":
|
| 56 |
-
topic
|
| 57 |
-
|
| 58 |
-
language = request.form.get("language", "English")
|
| 59 |
if topic:
|
| 60 |
-
result = generate_blog(topic
|
|
|
|
| 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__":
|
| 69 |
-
app.run(debug=True
|
|
|
|
| 3 |
from huggingface_hub import InferenceClient
|
| 4 |
import os
|
| 5 |
|
|
|
|
| 6 |
load_dotenv()
|
| 7 |
|
| 8 |
+
app = Flask(__name__)
|
| 9 |
+
|
| 10 |
client = InferenceClient(
|
|
|
|
| 11 |
api_key=os.getenv("HF_API_KEY")
|
| 12 |
)
|
| 13 |
|
|
|
|
|
|
|
|
|
|
| 14 |
MODELS = [
|
| 15 |
"mistralai/Mistral-7B-Instruct-v0.3",
|
| 16 |
"HuggingFaceH4/zephyr-7b-beta",
|
|
|
|
| 17 |
]
|
| 18 |
|
| 19 |
+
def generate_blog(topic):
|
| 20 |
+
prompt = f"""
|
| 21 |
+
Write a professional and engaging blog paragraph about:
|
| 22 |
+
{topic}
|
| 23 |
+
|
| 24 |
+
Blog:
|
| 25 |
+
"""
|
| 26 |
|
| 27 |
for model in MODELS:
|
| 28 |
try:
|
| 29 |
response = client.text_generation(
|
| 30 |
model=model,
|
| 31 |
prompt=prompt,
|
| 32 |
+
max_new_tokens=250,
|
| 33 |
temperature=0.7,
|
| 34 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
+
return str(response).strip()
|
| 37 |
|
| 38 |
+
except Exception as e:
|
| 39 |
+
print(f"Model failed: {model}")
|
| 40 |
+
print(e)
|
| 41 |
+
continue
|
| 42 |
+
|
| 43 |
+
return "Error: Could not generate blog."
|
| 44 |
|
| 45 |
@app.route("/", methods=["GET", "POST"])
|
| 46 |
def index():
|
| 47 |
paragraph = ""
|
| 48 |
error = ""
|
| 49 |
+
|
| 50 |
if request.method == "POST":
|
| 51 |
+
topic = request.form.get("topic", "").strip()
|
| 52 |
+
|
|
|
|
| 53 |
if topic:
|
| 54 |
+
result = generate_blog(topic)
|
| 55 |
+
|
| 56 |
if result.startswith("Error"):
|
| 57 |
error = result
|
| 58 |
else:
|
| 59 |
paragraph = result
|
|
|
|
| 60 |
|
| 61 |
+
return render_template(
|
| 62 |
+
"index.html",
|
| 63 |
+
paragraph=paragraph,
|
| 64 |
+
error=error
|
| 65 |
+
)
|
| 66 |
|
| 67 |
if __name__ == "__main__":
|
| 68 |
+
app.run(debug=True)
|