TaskBot-App / app.py
Advay-Singh's picture
Update app.py
2c4a62e verified
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)