File size: 4,181 Bytes
33dd9f4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c263e0d
 
33dd9f4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4e15ae5
 
 
 
33dd9f4
 
 
 
 
c263e0d
2a66080
33dd9f4
 
 
 
 
 
 
 
 
 
 
7ee21fa
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# **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)