Spaces:
DIVY118
/
Runtime error

File size: 3,462 Bytes
ad89eb2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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

app = Flask(__name__)


# Tracking API usage
counter={
    'Mistral7B': 0,
    'ChatGpt': 0,
    'News': 0,
    "Web": 0,
}

def Load():
    global counter
    current_datetime = datetime.now()

    # Extract only the date
    current_date = str(current_datetime.date())
    file=listdir(r"static/data/")
    if current_date in file:
        with open(r"static/data/"+current_date,"r") as f:
            counter=eval(f.read())
    else:
        counter={
            'Mistral7B': 0,
            'ChatGpt': 0,
            'News': 0,
            "Web": 0,
        }
        with open(r"static/data/"+current_date,"w") as f:
            f.write(str(counter))

def Update():
    # Get the current date and time
    global counter
    current_datetime = datetime.now()

    # Extract only the date
    current_date = str(current_datetime.date())
    file=listdir(r"static/data/")
    if current_date in file:
        with open(r"static/data/"+current_date,"w") as f:
            f.write(str(counter))
    else:
        counter={
            'Mistral7B': 0,
            'ChatGpt': 0,
            'News': 0,
            "Web": 0,
        }
        with open(r"static/data/"+current_date,"w") as f:
            f.write(str(counter))

@app.route('/mistral7b', methods=['POST'])
def generate():
    global counter
    # 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
    }
    counter['Mistral7B']+=1
    Update()
    return jsonify(result)

@app.route('/chatgpt', methods=['POST'])
def chat():
    global counter
    # 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
    }
    counter["ChatGpt"]+=1
    Update()
    return jsonify(result)

@app.route('/news', methods=['GET'])
def get_news():
    global counter
    # 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
    }
    counter["News"]+=1
    Update()
    return jsonify(result)

@app.route('/web', methods=['GET'])
def Web():
    key = request.args.get('prompt', '')
    result = {
        'response': Online_Scraper(key)
    }
    counter["Web"]+=1
    return jsonify(result)


@app.route('/divyanshpizza', methods=['GET'])
def get_counters():
    global counter
    return jsonify(counter),jsonify({"data":str(listdir(r"static/data/"))})

Load()

if __name__ == '__main__':
    app.run()