File size: 1,745 Bytes
cf51669
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
40
41
42
43
44
45
46
47
48
49
50
51
52
from flask import Flask, jsonify, request,render_template
from flask_cors import CORS
from bs4 import BeautifulSoup
import google.generativeai as genai

app = Flask(__name__)
CORS(app)

# Configure and initialize your AI model
genai.configure(api_key="AIzaSyCZVlIW9i4nCb7EDLdnfjhGGcMpELoCGSo")
model = genai.GenerativeModel('gemini-1.5-flash')

@app.route('/', methods=['GET', 'POST'])
def home():
    return render_template('popup.html')

def generate_ai_response(input_text):
    try:
        # Start a new chat session with the AI model
        chat = model.start_chat(history=[])
        response = chat.send_message(input_text + " \n Generate overview of text")
        print("AI Response:", response.text)
        return response.text
    except Exception as e:
        print("Error generating AI response:", str(e))
        return "Error generating AI response."

@app.route('/generate_summary', methods=['POST'])
def generate_summary():
    try:
        data = request.json
        
        html_content = data.get('html_content', '')
        js_content = data.get('js_content', '')

        # Remove HTML tags using BeautifulSoup
        soup_html = BeautifulSoup(html_content, 'html.parser')
        text_only_html = soup_html.get_text(separator=' ')
        
        # Combine processed HTML and JS content for AI input
        combined_text = f"{text_only_html} {js_content}"
        print(combined_text)
        # Generate AI response based on combined text
        ai_response = generate_ai_response(combined_text)

        return jsonify({"ai_response": ai_response})
    except Exception as e:
        return jsonify({"error": str(e)})


if __name__ == "__main__":
    app.run()