import requests from flask import Flask, render_template, request import speech_recognition as sr app = Flask(__name__) with open('i.txt', 'r') as file: data = file.read() API_URL = "https://api-inference.huggingface.co/models/mistralai/Mixtral-8x7B-Instruct-v0.1" headers = {"Authorization": f"Bearer hf{data}"} recognizer = sr.Recognizer() def query(payload): response = requests.post(API_URL, headers=headers, json=payload) return response.json() conversation_history = [] def generate_response(user_input): new_query = { "inputs": f"you are ai for help in anything you are created by Mr,Omar Nuwara he is made you \n\n make sure to help people in anything \n\ntask:complete the reesponse:\n\nconversation history:{conversation_history}\n\nuser message:{user_input}\n\nmake sure to response about it and don't generate alot of words just based on the user message \n\n\n\nresponse:", "parameters": { "top_k": 50, "top_p": 0.9, "temperature": 0.1, "repetition_penalty": 1.2, "max_new_tokens": 512, "max_time": 0, "return_full_text": True, "num_return_sequences": 1, "do_sample": False }, "options": { "use_cache": False, "wait_for_model": False } } output = query(new_query) generated_text = output[0]['generated_text'] response_start = generated_text.find('response:') + len('response:') response_end = generated_text.find('(end response)') response_text = generated_text[response_start:response_end].strip() note_index = response_text.find("Note:") if note_index != -1: response_text = response_text[:note_index].strip() instruction_index = response_text.find("### Instruction:") if instruction_index != -1: response_text = response_text[:instruction_index].strip() return response_text @app.route('/') def index(): return render_template('index.html') @app.route('/chat', methods=['POST']) def chat(): user_input = request.form['user_input'] # Generate AI response based on user input response_text = generate_response(user_input) conversation_history.append({"User": user_input, "AI": response_text}) return response_text if __name__ == '__main__': app.run(debug=True)