File size: 1,491 Bytes
3997312
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a6bfe90
3997312
 
 
 
 
 
 
 
 
 
eb29620
63ddb39
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#Downloading and importing all the modules required for this TaskBot v1 AI
import google.generativeai as genai
import os
import json
import requests
from flask import Flask, request, jsonify, render_template
import torch

#Connect to index.html
app = Flask(__name__)
@app.route("/")
def index():
    return render_template("index.html")

@app.route("/ask", methods=["POST"])
def ask():
    #getting the question from the form
    question = request.form.get("question", "").strip()
    if not question:
        return jsonify({"error": "Please provide a question."}), 400

    genai.configure(api_key="AIzaSyA5FFcaVCfVmwf7X5C59n4xWc96xWR-A_4")

    try:
        # use Google's Gemini-2.0-Flash nodle for generating content
        model = genai.GenerativeModel('gemini-2.0-flash')
        response = model.generate_content(f"You are TaskBot AI created and trained by Advay Singh, remember that and just anser me this question- {question}. Don't replay anything on that message just answer me the question.")
        answer = response.text

        # Log the question and answer for debugging
        print(f"Question: {question}\n------------------------- \n {answer} \n -------------------------")
        # Return the answer as JSON
        return jsonify({"answer": answer})
    except Exception as e:
        print(f"Error: {e}")
        return jsonify({"error": "An error occurred while processing your request."}), 500

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=7860)