Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,29 +3,33 @@ from dotenv import load_dotenv
|
|
| 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 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
|
| 30 |
Blog:
|
| 31 |
"""
|
|
@@ -35,28 +39,32 @@ Blog:
|
|
| 35 |
response = client.text_generation(
|
| 36 |
model=model,
|
| 37 |
prompt=prompt,
|
| 38 |
-
max_new_tokens=
|
| 39 |
temperature=0.7,
|
| 40 |
)
|
| 41 |
|
| 42 |
return str(response).strip()
|
| 43 |
|
| 44 |
except Exception as e:
|
| 45 |
-
print(f"
|
| 46 |
-
print(e)
|
| 47 |
continue
|
| 48 |
|
| 49 |
-
return "Error:
|
|
|
|
| 50 |
|
|
|
|
| 51 |
@app.route("/", methods=["GET", "POST"])
|
| 52 |
def index():
|
|
|
|
| 53 |
paragraph = ""
|
| 54 |
error = ""
|
| 55 |
|
| 56 |
if request.method == "POST":
|
|
|
|
| 57 |
topic = request.form.get("topic", "").strip()
|
| 58 |
|
| 59 |
if topic:
|
|
|
|
| 60 |
result = generate_blog(topic)
|
| 61 |
|
| 62 |
if result.startswith("Error"):
|
|
@@ -70,5 +78,7 @@ def index():
|
|
| 70 |
error=error
|
| 71 |
)
|
| 72 |
|
|
|
|
|
|
|
| 73 |
if __name__ == "__main__":
|
| 74 |
app.run(debug=True)
|
|
|
|
| 3 |
from huggingface_hub import InferenceClient
|
| 4 |
import os
|
| 5 |
|
| 6 |
+
# Load environment variables
|
| 7 |
load_dotenv()
|
| 8 |
|
| 9 |
app = Flask(__name__)
|
| 10 |
|
| 11 |
+
# Initialize Hugging Face client
|
| 12 |
client = InferenceClient(
|
| 13 |
api_key=os.getenv("HF_API_KEY")
|
| 14 |
)
|
| 15 |
|
| 16 |
+
# Models to try
|
| 17 |
MODELS = [
|
| 18 |
"mistralai/Mistral-7B-Instruct-v0.3",
|
| 19 |
"HuggingFaceH4/zephyr-7b-beta",
|
| 20 |
]
|
| 21 |
|
| 22 |
+
# Generate blog function
|
| 23 |
def generate_blog(topic):
|
| 24 |
+
|
| 25 |
+
prompt = f"""
|
| 26 |
+
Write a detailed and engaging blog article on the topic: "{topic}"
|
| 27 |
+
|
| 28 |
+
The blog should include:
|
| 29 |
+
- A catchy title
|
| 30 |
+
- Introduction
|
| 31 |
+
- Main content
|
| 32 |
+
- Conclusion
|
| 33 |
|
| 34 |
Blog:
|
| 35 |
"""
|
|
|
|
| 39 |
response = client.text_generation(
|
| 40 |
model=model,
|
| 41 |
prompt=prompt,
|
| 42 |
+
max_new_tokens=500,
|
| 43 |
temperature=0.7,
|
| 44 |
)
|
| 45 |
|
| 46 |
return str(response).strip()
|
| 47 |
|
| 48 |
except Exception as e:
|
| 49 |
+
print(f"Error with model {model}: {e}")
|
|
|
|
| 50 |
continue
|
| 51 |
|
| 52 |
+
return "Error: All models failed."
|
| 53 |
+
|
| 54 |
|
| 55 |
+
# Home Route
|
| 56 |
@app.route("/", methods=["GET", "POST"])
|
| 57 |
def index():
|
| 58 |
+
|
| 59 |
paragraph = ""
|
| 60 |
error = ""
|
| 61 |
|
| 62 |
if request.method == "POST":
|
| 63 |
+
|
| 64 |
topic = request.form.get("topic", "").strip()
|
| 65 |
|
| 66 |
if topic:
|
| 67 |
+
|
| 68 |
result = generate_blog(topic)
|
| 69 |
|
| 70 |
if result.startswith("Error"):
|
|
|
|
| 78 |
error=error
|
| 79 |
)
|
| 80 |
|
| 81 |
+
|
| 82 |
+
# Run app
|
| 83 |
if __name__ == "__main__":
|
| 84 |
app.run(debug=True)
|