Spaces:
Sleeping
Sleeping
ParthCodes
commited on
Commit
•
73e95e8
1
Parent(s):
792d54d
Upload 3 files
Browse files- Dockerfile +13 -0
- app.py +163 -0
- requirements.txt +4 -0
Dockerfile
ADDED
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
FROM python:3.9
|
2 |
+
|
3 |
+
WORKDIR /code
|
4 |
+
|
5 |
+
COPY ./requirements.txt /code/requirements.txt
|
6 |
+
|
7 |
+
RUN pip install --no-cache-dir --upgrade -r requirements.txt
|
8 |
+
|
9 |
+
RUN pip install -U g4f
|
10 |
+
|
11 |
+
COPY . /code
|
12 |
+
|
13 |
+
CMD ["gunicorn", "main:app", "-b", "0.0.0.0:7860"]
|
app.py
ADDED
@@ -0,0 +1,163 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from flask import Flask, jsonify, request
|
2 |
+
from flask_cors import CORS
|
3 |
+
import string
|
4 |
+
import random
|
5 |
+
import g4f
|
6 |
+
|
7 |
+
app = Flask(__name__)
|
8 |
+
|
9 |
+
CORS(app)
|
10 |
+
|
11 |
+
rooms = {}
|
12 |
+
room_data = {}
|
13 |
+
group_chat = {'chats': []}
|
14 |
+
|
15 |
+
|
16 |
+
@app.route('/')
|
17 |
+
def index():
|
18 |
+
return "Hello 👋, This is CrickTech Stream API's Hub."
|
19 |
+
|
20 |
+
|
21 |
+
def generate_room_code():
|
22 |
+
return ''.join(random.choices(string.ascii_uppercase, k=6))
|
23 |
+
|
24 |
+
|
25 |
+
@app.route('/create-room', methods=['POST'])
|
26 |
+
def create_room():
|
27 |
+
room_code = generate_room_code()
|
28 |
+
rooms[room_code] = {'players': [], 'started': True, 'process': 0, 'generated': False, 'story_content': '',
|
29 |
+
'reactions': 0, 'chats': [], 'pollCount': 0}
|
30 |
+
|
31 |
+
player_name = request.json.get('name')
|
32 |
+
if not player_name:
|
33 |
+
return jsonify({'error': 'Player name is required'})
|
34 |
+
|
35 |
+
# Add the player to the room.
|
36 |
+
player_id = len(rooms[room_code]['players']) + 1
|
37 |
+
rooms[room_code]['players'].append({'id': player_id, 'name': player_name})
|
38 |
+
|
39 |
+
return jsonify({'room_code': room_code})
|
40 |
+
|
41 |
+
|
42 |
+
@app.route('/join-room/<room_code>', methods=['POST', 'GET'])
|
43 |
+
def join_room(room_code):
|
44 |
+
if request.method == 'POST':
|
45 |
+
if room_code not in rooms:
|
46 |
+
return jsonify({'noRoom': True})
|
47 |
+
|
48 |
+
player_name = request.json.get('name')
|
49 |
+
if not player_name:
|
50 |
+
return jsonify({'error': 'Player name is required'})
|
51 |
+
|
52 |
+
# Add the player to the room.
|
53 |
+
player_id = len(rooms[room_code]['players']) + 1
|
54 |
+
rooms[room_code]['players'].append({'id': player_id, 'name': player_name})
|
55 |
+
|
56 |
+
return jsonify({'player_id': player_id, 'players': rooms[room_code]['players']})
|
57 |
+
|
58 |
+
if request.method == 'GET':
|
59 |
+
if room_code not in rooms:
|
60 |
+
return jsonify({'noRoom': True})
|
61 |
+
|
62 |
+
ready = False
|
63 |
+
|
64 |
+
if len(rooms[room_code]['players']) == 5: # do it <= 5
|
65 |
+
ready = True
|
66 |
+
|
67 |
+
return jsonify({'players': rooms[room_code]['players'], 'ready': ready, 'started': rooms[room_code]['started'],
|
68 |
+
'meta_data': rooms[room_code]})
|
69 |
+
|
70 |
+
|
71 |
+
@app.route('/leave-room/<room_code>/<int:player_id>', methods=['POST'])
|
72 |
+
def leave_room(room_code, player_id):
|
73 |
+
if room_code not in rooms:
|
74 |
+
return jsonify({'error': 'Room not found'})
|
75 |
+
|
76 |
+
rooms[room_code]['players'] = [player for player in rooms[room_code]['players'] if player['id'] != player_id]
|
77 |
+
|
78 |
+
if player_id == 1 or not rooms[room_code]['players']:
|
79 |
+
del rooms[room_code]
|
80 |
+
|
81 |
+
return jsonify({'message': 'Player has left the room'})
|
82 |
+
|
83 |
+
|
84 |
+
@app.route('/reaction-true/<room_code>', methods=['GET'])
|
85 |
+
def reaction_true(room_code):
|
86 |
+
rooms[room_code]['reactions'] += 1
|
87 |
+
|
88 |
+
return jsonify({'success': True, 'likeCount': rooms[room_code]['reactions']})
|
89 |
+
|
90 |
+
|
91 |
+
@app.route('/reaction-false/<room_code>', methods=['GET'])
|
92 |
+
def reaction_false(room_code):
|
93 |
+
rooms[room_code]['reactions'] -= 1
|
94 |
+
|
95 |
+
return jsonify({'success': True, 'likeCount': rooms[room_code]['reactions']})
|
96 |
+
|
97 |
+
|
98 |
+
@app.route('/room-chat/<room_code>', methods=['GET'])
|
99 |
+
def room_chat(room_code):
|
100 |
+
if room_code not in rooms:
|
101 |
+
return jsonify({'success': False, 'error': 'Room not found'})
|
102 |
+
print(rooms[room_code]['chats'])
|
103 |
+
return jsonify({'success': True, 'chats': rooms[room_code]['chats'], 'pollCount': rooms[room_code]['pollCount']})
|
104 |
+
|
105 |
+
|
106 |
+
@app.route('/chat-input/<room_code>/<int:player_id>', methods=['POST'])
|
107 |
+
def chat_input(room_code, player_id):
|
108 |
+
data = request.get_json()
|
109 |
+
if room_code not in rooms:
|
110 |
+
return jsonify({'success': False, 'error': 'Room not found'})
|
111 |
+
|
112 |
+
rooms[room_code]['chats'].append({'id': player_id, 'text': data.get('text'), 'playerName': data.get('playerName')})
|
113 |
+
|
114 |
+
return jsonify({'success': True})
|
115 |
+
|
116 |
+
|
117 |
+
@app.route('/poll-data/<room_code>', methods=['POST'])
|
118 |
+
def poll_count(room_code):
|
119 |
+
if room_code not in rooms:
|
120 |
+
return jsonify({'success': False, 'error': 'Room not found'})
|
121 |
+
|
122 |
+
rooms[room_code]['pollCount'] += 1
|
123 |
+
|
124 |
+
return jsonify({'success': True, 'pollCount': rooms[room_code]['pollCount']})
|
125 |
+
|
126 |
+
|
127 |
+
@app.route('/list-rooms', methods=['GET'])
|
128 |
+
def list_rooms():
|
129 |
+
room_info = {}
|
130 |
+
for room_code, player in rooms.items():
|
131 |
+
room_info[room_code] = len(player['players'])
|
132 |
+
|
133 |
+
return jsonify({'roomInfo': room_info})
|
134 |
+
|
135 |
+
|
136 |
+
@app.route('/group-chat', methods=['POST', 'GET'])
|
137 |
+
def groupchat():
|
138 |
+
if request.method == 'POST':
|
139 |
+
data = request.get_json()
|
140 |
+
group_chat['chats'].append({'id': data.get('playerName'), 'text': data.get('text')})
|
141 |
+
return jsonify({'success': True})
|
142 |
+
|
143 |
+
if request.method == 'GET':
|
144 |
+
print(group_chat['chats'])
|
145 |
+
return jsonify({'success': True, 'chats': group_chat['chats']})
|
146 |
+
|
147 |
+
@app.route('/chat-bot', methods=['POST'])
|
148 |
+
def chatbot():
|
149 |
+
if request.method == 'POST':
|
150 |
+
data = request.get_json()
|
151 |
+
query = data.get('query')
|
152 |
+
|
153 |
+
response = g4f.ChatCompletion.create(
|
154 |
+
model="gpt-3.5-turbo",
|
155 |
+
provider=g4f.Provider.Hashnode,
|
156 |
+
messages=[{"role": "user", "content": query}],
|
157 |
+
)
|
158 |
+
|
159 |
+
return jsonify({'success': True, 'response': '#' + str(response)[1:]})
|
160 |
+
|
161 |
+
|
162 |
+
if __name__ == '__main__':
|
163 |
+
app.run()
|
requirements.txt
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
flask
|
2 |
+
flask-cors
|
3 |
+
gunicorn
|
4 |
+
Jinja2
|