Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from flask import Flask, render_template, request, jsonify
|
| 2 |
+
from database import create_database, get_all_users, get_vacation_location
|
| 3 |
+
from langchain_core.prompts import ChatPromptTemplate
|
| 4 |
+
from langchain_ollama.llms import OllamaLLM
|
| 5 |
+
|
| 6 |
+
# Initialize Flask app
|
| 7 |
+
app = Flask(__name__)
|
| 8 |
+
|
| 9 |
+
# Initialize the database
|
| 10 |
+
create_database()
|
| 11 |
+
|
| 12 |
+
# Define the template for generating a vacation-related question
|
| 13 |
+
template = """You are planning a vacation. Based on the location '{location}', what would be a good question to ask the vacationer to learn more about their vacation plans?"""
|
| 14 |
+
|
| 15 |
+
# Initialize the prompt and model using LangChain
|
| 16 |
+
prompt = ChatPromptTemplate.from_template(template)
|
| 17 |
+
#model = OllamaLLM(model="llama3.1")
|
| 18 |
+
model = OllamaLLM(model="llama3.2:1b")
|
| 19 |
+
# Define the chain using the prompt and model
|
| 20 |
+
chain = prompt | model
|
| 21 |
+
|
| 22 |
+
@app.route('/')
|
| 23 |
+
def index():
|
| 24 |
+
"""Render the homepage with a dropdown of usernames."""
|
| 25 |
+
users = get_all_users()
|
| 26 |
+
return render_template('index.html', users=users)
|
| 27 |
+
|
| 28 |
+
@app.route('/vacation', methods=['POST'])
|
| 29 |
+
def vacation():
|
| 30 |
+
"""Display the vacation location without AI interaction."""
|
| 31 |
+
user_id = request.form.get('user_id')
|
| 32 |
+
|
| 33 |
+
if not user_id:
|
| 34 |
+
return "User ID is missing", 400
|
| 35 |
+
|
| 36 |
+
location = get_vacation_location(user_id)
|
| 37 |
+
username = next((user[1] for user in get_all_users() if user[0] == int(user_id)), None)
|
| 38 |
+
|
| 39 |
+
if not username:
|
| 40 |
+
return "User not found", 404
|
| 41 |
+
|
| 42 |
+
return render_template('vacation.html', username=username, location=location)
|
| 43 |
+
|
| 44 |
+
@app.route('/generate_question', methods=['POST'])
|
| 45 |
+
def generate_question():
|
| 46 |
+
"""Generate a vacation-related question using LangChain."""
|
| 47 |
+
data = request.json # Read the JSON payload from the POST request
|
| 48 |
+
print("Received data:", data) # Debugging
|
| 49 |
+
|
| 50 |
+
location = data.get('location')
|
| 51 |
+
print("Extracted location:", location) # Debugging
|
| 52 |
+
|
| 53 |
+
if not location:
|
| 54 |
+
return jsonify({"error": "Location is missing"}), 400
|
| 55 |
+
|
| 56 |
+
try:
|
| 57 |
+
# Generate the question using LangChain
|
| 58 |
+
question = chain.invoke({"location": location})
|
| 59 |
+
print("Generated question:", question) # Debugging
|
| 60 |
+
except Exception as e:
|
| 61 |
+
print("Error during question generation:", e) # Debugging
|
| 62 |
+
return jsonify({"error": f"Failed to generate question: {str(e)}"}), 500
|
| 63 |
+
|
| 64 |
+
return jsonify({"question": question}), 200
|
| 65 |
+
|
| 66 |
+
if __name__ == '__main__':
|
| 67 |
+
app.run(debug=True)
|