from flask import Flask, request, render_template, jsonify, Response, stream_with_context
import json
from groq import Groq
app = Flask(__name__)
openai_api_key="sk-proj-Y3oUsYZq3xucUHtIWE4qT3BlbkFJIDWer5L88OUmeK95l9bJ"
api_key = "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"  # Replace this with your actual API key
client = Groq(api_key=api_key)

# Route to serve the HTML interface
@app.route('/')
def index():
    return render_template('index.html')

# Store user message to be processed
user_message_store = []

@app.route('/process_chat', methods=['POST'])
def process_chat():
    user_message = request.json['user_message']
    print("usermessage: ", user_message)
    user_message_store.append(user_message)
    return jsonify({"status": "success"})

@app.route('/stream_response')
def stream_response():
    @stream_with_context
    def generate():
        if user_message_store:
            user_message = user_message_store.pop(0)
            completion = client.chat.completions.create(
                model="llama3-8b-8192",
                messages=[
                    {
                        "role": "system",
                        "content": "You are a helpful Assistant.You provide answer in bengali language, not write english language."
                    },
                    {
                        "role": "user",
                        "content": user_message,
                    }
                ],
                temperature=1,
                max_tokens=1024,
                top_p=1,
                stream=True,
                stop=None,
            )
            for chunk in completion:
                content = chunk.choices[0].delta.content or ""
                print(content)
                yield f"data: {json.dumps({'response_message': content})}\n\n"
        else:
            yield f"data: {json.dumps({'response_message': ''})}\n\n"

    return Response(generate(), mimetype='text/event-stream')

if __name__ == '__main__':
    app.run(debug=True)
