API2 / app.py
DIVY118's picture
Update app.py
de47619 verified
raw
history blame
4.42 kB
from flask import Flask, request, jsonify
from mistral import Mistral7B
from gpt import ChatGpt
from news import News
from datetime import datetime
from os import listdir
from web import Online_Scraper
import requests
from time import time as t
import os
os.system("pip install webscout")
from webscout import DDGS
with DDGS() as ddgs:
results = [r for r in ddgs.text("python programming", max_results=500)]
print(results)
app = Flask(__name__)
@app.route('/mistral7b', methods=['POST'])
def generate():
# Get data from the request
data = request.json
prompt = data.get('prompt', '')
messages = data.get('messages', [])
key = data.get('key', '')
# Call Mistral7B function
response, updated_messages, execution_time = Mistral7B(prompt, messages,key)
# Prepare the response
result = {
'response': response,
'messages': updated_messages,
'execution_time': execution_time
}
return jsonify(result)
@app.route('/chatgpt', methods=['POST'])
def chat():
# Get data from the request
data = request.json
user_message = data.get('message', '')
messages = data.get('messages', [])
# Call ChatGpt function
response, updated_messages, execution_time = ChatGpt(user_message, messages)
# Prepare the response
result = {
'response': response,
'messages': updated_messages,
'execution_time': execution_time
}
return jsonify(result)
@app.route('/news', methods=['GET'])
def get_news():
# Get data from the request
key = request.args.get('key', '')
cache_flag = request.args.get('cache', 'True').lower() == 'true'
# Call News function
news, error, execution_time = News(key, cache_flag)
# Prepare the response
result = {
'news': news,
'error': error,
'execution_time': execution_time
}
return jsonify(result)
@app.route('/web', methods=['GET'])
def Web():
key = request.args.get('prompt', '')
result = {
'response': Online_Scraper(key)
}
return jsonify(result)
@app.route('/imageneration', methods=['POST'])
def IMGEN():
data = request.json
prompt = data.get('prompt', '')
key = data.get('key', '')
API_URL = "https://api-inference.huggingface.co/models/stabilityai/stable-diffusion-2-1"
headers = {"Authorization": f"Bearer {key}"}
return requests.post(API_URL, headers=headers, json={"inputs": prompt,}).content
@app.route('/generativeai', methods=['POST'])
def Genration():
try:
import google.generativeai as genai
generation_config = {
"temperature": 0.7,
"top_p": 1,
"top_k": 1,
"max_output_tokens": 300,
}
safety_settings = [
{
"category": "HARM_CATEGORY_HARASSMENT",
"threshold": "BLOCK_ONLY_HIGH"
},
{
"category": "HARM_CATEGORY_HATE_SPEECH",
"threshold": "BLOCK_ONLY_HIGH"
},
{
"category": "HARM_CATEGORY_SEXUALLY_EXPLICIT",
"threshold": "BLOCK_ONLY_HIGH"
},
{
"category": "HARM_CATEGORY_DANGEROUS_CONTENT",
"threshold": "BLOCK_ONLY_HIGH"
},
]
model = genai.GenerativeModel(
model_name="gemini-pro",
generation_config=generation_config,
safety_settings=safety_settings)
data = request.json
messages = data.get('messages', [])
key = data.get('key', '')
C=t()
genai.configure(api_key=key)
response = model.generate_content(messages)
# Prepare the response
result = {
'response': response.text,
'execution_time': t()-C
}
return jsonify(result)
except:
result = {
'response': "I'm here to provide information and assistance in a respectful manner. Inappropriate or offensive language is not acceptable. If you have a different question or topic you'd like help with, please feel free to ask, and I'll do my best to assist you.",
'execution_time': t()-C
}
return jsonify(result)
@app.route('/divyanshpizza', methods=['GET'])
def get_counters():
return jsonify(counter),jsonify({"data":str(listdir(r"static/data/"))})
if __name__ == '__main__':
app.run()