Below you'll find step-by-step instructions and working Python code to create a Flask API and GUI-driven app that generates motivational quotes using a Large Language Model (LLM), like LLaMA, for Hindi and English audiences. Since we do not have direct access to LLaMA or its API in this example, we will mock its behavior and simulate it using Python. You can replace the mock function later with an actual LLaMA API or integration.
The app will provide an API endpoint and a basic web-based GUI to generate the quotes.
Prerequisites
- Install Flask and Flask-WTF:
pip install flask flask-wtf
- If you are using an LLM API, ensure you install its client library.
Code for Flask API & GUI
- Set up your Python Flask API and GUI: Save the following code in a file, e.g.,
app.py.
from flask import Flask, request, jsonify, render_template
import random
# Mock example of the LLM (replace it with actual LLaMA integration)
def generate_motivation_quote(language="en"):
english_quotes = [
"Believe in yourself and all that you are. Know that there is something inside you that is greater than any obstacle.",
"Your future is created by what you do today, not tomorrow.",
"Success is not final, failure is not fatal: it is the courage to continue that counts.",
]
hindi_quotes = [
"अपने आप पर विश्वास करें और उस पर जो आप हैं। जान लें कि आपके अंदर कुछ है जो किसी भी बाधा से बड़ा है।",
"आपका भविष्य इससे बनता है जो आप आज करते हैं, न कि कल।",
"सफलता अंतिम नहीं है, विफलता घातक नहीं है: यह हिम्मत है जो मायने रखती है।",
]
if language == "en":
return random.choice(english_quotes)
elif language == "hi":
return random.choice(hindi_quotes)
else:
return "Language not supported."
# Flask app
app = Flask(__name__)
app.config['SECRET_KEY'] = 'your-secret-key'
@app.route('/api/motivation', methods=['POST'])
def generate_quote_api():
data = request.json
language = data.get("language", "en") # Get the language from the request (default: English)
# Use a Large Language Model (mock function here)
quote = generate_motivation_quote(language)
return jsonify({"quote": quote})
@app.route("/", methods=["GET", "POST"])
def index():
quote = None
if request.method == "POST":
# Get user input from the form
language = request.form["language"]
# Use the LLM to generate the quote
quote = generate_motivation_quote(language)
return render_template("index.html", quote=quote)
if __name__ == "__main__":
app.run(debug=True)
- Create the HTML template for the GUI: Save the following code in a file named
templates/index.html.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Motivational Quotes Generator</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 20px;
}
.container {
max-width: 600px;
margin: auto;
text-align: center;
}
textarea {
margin-top: 20px;
width: 100%;
height: 100px;
resize: none;
}
</style>
</head>
<body>
<div class="container">
<h1>Motivational Quotes Generator</h1>
<form method="post">
<label for="language">Choose language:</label>
<select name="language" id="language">
<option value="en">English</option>
<option value="hi">Hindi</option>
</select>
<br><br>
<button type="submit">Generate Quote</button>
</form>
{% if quote %}
<h3>Your Motivational Quote:</h3>
<textarea readonly>{{ quote }}</textarea>
{% endif %}
</div>
</body>
</html>
- Running the application:
Save the above code in the same directory. Run the Flask app using:
python app.py
Open your browser and go to http://127.0.0.1:5000. You’ll see the web-based GUI for generating motivational quotes.
Explanation of the Workflow:
API Endpoint (/api/motivation)
- POST Request: Accepts JSON data with a "language" key (e.g.,
"en" for English or "hi" for Hindi).
- Response: Returns a motivational quote generated by the mocked LLM.
GUI (/)
- Displays a simple HTML form to choose the language and get the quotes.
- On form submission, the app processes the selected language, gets the motivational quote from the mocked LLM function, and shows it on the page.
To-Do for Real-World Use:
- Replace the
generate_motivation_quote mock function with actual LLaMA (or another LLM-based) integration. You can use libraries like transformers from Hugging Face to load and query your own LLaMA model or an alternative pre-trained model.
- Add support for more languages.
- Deploy the Flask app using a production-ready server like
Gunicorn or uWSGI and Docker if needed.
Let me know if you need help integrating the real LLaMA model! 🚀