Durgaai-Solutions's picture
Update app.py
c263e0d verified
# **Durgaai Solutions (India First AI Assistant)**
# **A Flask API for generating text using the Mixtral-7B-Instruct-v0.2 model**
import uvicorn
from flask import Flask, request, jsonify, render_template_string
from huggingface_hub import InferenceClient
import logging
# Configure logging
logging.basicConfig(level=logging.INFO)
# Defining Application
app = Flask(__name__)
# Defining Model Used
used_model = "https://api-inference.huggingface.co/models/mistralai/Mixtral-8x7B-Instruct-v0.1"
# Defining Prompt
def customize_prompt(message, final_instructions=None):
prompt = ""
if final_instructions:
prompt += f"[INST] {final_instructions} [/INST]"
prompt += f"[INST] {message} [/INST]"
return prompt
# Main Application
def public_model(prompt, instructions, api, temperature=0.90, max_new_tokens=256, top_p=0.95, repetition_penalty=1.2):
global used_model
try:
temperature = float(temperature)
if temperature < 1e-2:
temperature = 1e-2
top_p = float(top_p)
generate_kwargs = dict(
temperature=temperature,
max_new_tokens=max_new_tokens,
top_p=top_p,
repetition_penalty=repetition_penalty,
do_sample=True,
seed=69,
)
final_instructions = instructions
result = customize_prompt(prompt, final_instructions)
head = {"Authorization": f"Bearer {api}"}
client = InferenceClient(used_model, headers=head)
response = client.text_generation(result, **generate_kwargs)
return response
except Exception as e:
logging.error(f"Error generating text: {e}")
return str(e)
# Running Application
@app.route("/run-application", methods=["POST"])
def run_application():
data = request.json
prompt = data.get("prompt")
instructions = data.get("instructions")
api_key = data.get("api_key")
if not prompt or not instructions or not api_key:
return jsonify({"Error": "Missing Required Fields"}), 400
try:
# Validate API key
if not api_key.startswith("hf_"):
return jsonify({"Error": "Invalid API key"}), 401
response = public_model(prompt, instructions, api_key)
return jsonify({"Response": response}), 200
except Exception as e:
logging.error(f"Error processing request: {e}")
return jsonify({"Error": "Internal Server Error"}), 500
# Basic HTML Interface
html = '''
<!DOCTYPE html>
<html>
<head>
<title>Mixtral 7b Instruct v0.1 = Public Server For API Usage</title>
<style>
body {
font-family: Arial, sans-serif;
}
.Container {
text-align: center;
max-width: 800px;
margin: 40px;
padding: 12px;
background-color: #f9f9f9;
border: 4px solid lawngreen;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.2);
border-radius: 12px;
}
.Container h1 {
font-size: 40px;
color: orange;
}
.Container p {
font-size: 20px;
color: darkred;
}
.Container p0{
font-size: 24px;
color: darkmagenta;
}
.Container a{
font-size: 24px;
color: red;
}
</style>
</head>
<body>
<div class="Container">
<h1>Mixtral 7b Instruct v0.1 = Public Server</h1>
<p>Welcome To Durgaai Solutions Organisation ( To Use This Server Follow Given Steps )<br></br>1. Create Your Hugging Face Access Token From Your HF Account <br></br>2. Go To Our { Github Organisation | Source Code } Page By Link Given Below<br></br>3. Download The Respository Do Suggested Changes To (Main-Application.py)</p>
<a href="https://github.com/Durgaai-Solutions-Hub/Mixtral-7b-Instruct-v0.1" target="_blank">Github Link</a><p0> : Now Use It As Free With Unlimited Access To This AI Model</p0>
</div>
</body>
</html>
'''
# Launching Interface
@app.route("/", methods=["GET"])
def index():
return render_template_string(html)
# Launch Application
if __name__ == "__main__":
app.run(debug=True)