File size: 6,422 Bytes
e3974b3
07cf108
 
 
1f76cca
 
 
07cf108
 
2c4a62e
07cf108
 
 
 
 
 
1f76cca
 
 
07cf108
 
 
 
 
 
 
 
 
 
 
 
1f76cca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
07cf108
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1f76cca
 
 
 
07cf108
 
1f76cca
 
07cf108
 
 
 
 
 
 
 
 
 
 
1f76cca
 
 
 
 
 
 
 
 
 
 
07cf108
 
 
 
 
 
 
 
 
8080689
07cf108
 
 
 
1f76cca
07cf108
 
 
 
 
1f76cca
 
 
 
 
 
 
 
07cf108
 
 
 
 
 
 
 
 
 
 
1f76cca
 
 
 
 
 
 
 
07cf108
1f76cca
 
07cf108
 
 
 
 
 
 
 
 
 
 
 
 
1f76cca
07cf108
 
 
 
1f76cca
 
 
 
 
 
 
 
07cf108
 
 
 
 
 
1f76cca
 
 
 
 
07cf108
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1f76cca
 
 
 
 
 
07cf108
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import os
from openai import OpenAI
from flask import Flask, request, jsonify, render_template, send_file


app = Flask(__name__)

client = OpenAI(
    base_url="https://router.huggingface.co/v1",
    api_key=os.environ["HF_TOKEN"],
)

app = Flask(__name__)
@app.route("/")
def index():
    return render_template("index.html")

@app.route("/write", methods=["GET", "POST"])
def write():

    WRITE_PROMPT = """
You are TaskBot AI created by Advay Singh.

Task:
Write high-quality content based on the user's request.

Rules:
- Output only the requested content.
- Do not say "Here is your essay/speech".
- Do not chat with the user.
"""
    if request.method == "GET":
        return render_template("write.html")

    if request.method == "POST":
        # Getting data from form
        question = request.form.get("question", "").strip()
        types = request.form.get("type", "").strip()
        word_limit = request.form.get("word_limit", "").strip()

        if not question:
            return jsonify({"error": "Please provide a question."}), 400

        if word_limit:
            try:
                word_limit = float(word_limit)
            except ValueError:
                return jsonify({"error": "Word limit must be a number."}), 400
        else:
            word_limit = None

        try:
            completion = client.chat.completions.create(
            model="Qwen/Qwen2.5-7B-Instruct:together",
            messages=[
    {
        "role": "system",
        "content": WRITE_PROMPT
        },
            {
            "role": "user",
            "content": f"Write a {types if types else 'paragraph'} on the topic '{question} in about {word_limit} words.'"
            }
        ],
    )

            response = completion.choices[0].message.content
            print(f"WRITE ABOUT: \n{question} in {types} in minimum {word_limit}\n------------------------- \n AI RESPONSE: {response} \n--------------------------")
            return jsonify({"answer": response})
        
        except Exception as e:
            print(f"Error: {e}")
            return jsonify({"error": "An error occurred while processing your request."}), 500



@app.route("/summarize", methods=["GET", "POST"])
def summarize():
    SUMMARIZE_PROMPT = """
You are TaskBot AI created by Advay Singh.

Task:
Summarize the given text.

Rules:
- Output ONLY the summary.
- Do not add introductions like "Here is the summary".
- Do not chat with the user.
"""
    if request.method == "GET":
        return render_template("summarize.html")
    if request.method == "POST":
        question = request.form.get("question", "").strip()
    types = request.form.get("type")
    minimum_lines_points = request.form.get("num_of_lines_points")
    if not question:
        return jsonify({"error": "Please provide a question."}), 400

    try:

        completion = client.chat.completions.create(
            model="Qwen/Qwen2.5-7B-Instruct:together",
            messages=[
    {
        "role": "system",
        "content": SUMMARIZE_PROMPT
        },
            {
            "role": "user",
            "content": f"SUMMARIZE: \n{question} in {types} in minimum {minimum_lines_points} {types}"
            }
        ],
    )


        response = completion.choices[0].message.content
        
        print(f"SUMMARIZE: \n{question} in {types} in minimum {minimum_lines_points} {types}\n------------------------- \n AI RESPONSE: {response} \n--------------------------")

        return jsonify({"answer": response})

    except Exception as e:
        print(f"Error: {e}")
        return jsonify({"error": "An error occurred while processing your request."}), 500


@app.route("/translate", methods=["GET", "POST"])
def translate():
    TRANSLATE_PROMPT = """
You are TaskBot AI created by Advay Singh.

Task:
Translate the given text to the requested language.

Rules:
- Output ONLY the translated text.
- Do not add explanations.
- Do not say "Here is the translation" or something else like that, just give the answer directly.
"""
    if request.method == "GET":
        return render_template("translate.html")
    if request.method == "POST":
        question = request.form.get("question", "").strip()
        translate_from = request.form.get("translate_from", "").strip()
        translate_to = request.form.get("translate_to", "").strip()
        if not question:
            return jsonify({"error": "Please provide a question."}), 400
        
        try:

            completion = client.chat.completions.create(
            model="Qwen/Qwen2.5-7B-Instruct:together",
            messages=[
        {
        "role": "system",
        "content": TRANSLATE_PROMPT
        },
            {
            "role": "user",
            "content": f"translate {question} from {translate_from} to {translate_to}."
            }
        ],
    )

            response = completion.choices[0].message.content
            print(f"Translate: {question} from {translate_from} to {translate_to}\n------------------------- \n AI RESPONSE: {response} \n--------------------------")

            return jsonify({"answer": response})

        except Exception as e:
            print(f"Error: {e}")
            return jsonify({"error": "An error occurred while processing your request."}), 500


@app.route("/ask", methods=["POST"])
def ask():
    CHAT_PROMPT = """
You are TaskBot AI created by Advay Singh.

Respond normally and conversationally.
Never mention the underlying AI model.
"""
    question = request.form.get("question", "").strip()
    if not question:
        return jsonify({"error": "Please provide a question."}), 400

    try:
        completion = client.chat.completions.create(
            model="Qwen/Qwen2.5-7B-Instruct:together",
            messages=[
        {
        "role": "system",
        "content": CHAT_PROMPT
        },
            {
            "role": "user",
            "content": f"{question}"
            }
        ],
    )

        response = completion.choices[0].message.content
        print(f"QUESTION: \n{question} \n-------------------------------- AI RESPONSE: \n{response} \n --------------------------------")
        return jsonify({"answer": response})
    except Exception as e:
        print(f"Error: {e}")
        return jsonify({"error": "An error occurred while processing your request."}), 500

if __name__ == '__main__':

        app.run(host="0.0.0.0", port=7860)