File size: 13,328 Bytes
394915e
 
 
 
 
 
00ed5fb
394915e
 
 
00ed5fb
 
394915e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40b3207
394915e
 
b64c76c
394915e
 
 
 
40b3207
394915e
 
00ed5fb
 
394915e
 
 
 
00ed5fb
394915e
 
 
 
 
 
 
 
 
 
 
00ed5fb
394915e
 
 
40b3207
394915e
 
 
 
00ed5fb
394915e
00ed5fb
394915e
00ed5fb
394915e
 
 
 
 
00ed5fb
394915e
 
 
 
00ed5fb
394915e
 
 
 
 
 
 
00ed5fb
394915e
 
 
 
00ed5fb
394915e
00ed5fb
 
394915e
00ed5fb
 
394915e
00ed5fb
394915e
00ed5fb
 
 
394915e
 
 
00ed5fb
 
 
 
 
394915e
00ed5fb
394915e
00ed5fb
 
 
394915e
 
 
 
00ed5fb
 
394915e
 
 
 
 
 
00ed5fb
394915e
 
 
 
 
00ed5fb
394915e
 
 
00ed5fb
394915e
 
 
b64c76c
394915e
 
b64c76c
 
394915e
 
b64c76c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
394915e
 
 
00ed5fb
 
394915e
 
 
 
00ed5fb
 
394915e
 
 
 
 
 
 
 
 
 
 
 
 
40b3207
00ed5fb
394915e
 
 
00ed5fb
394915e
00ed5fb
 
 
 
394915e
00ed5fb
394915e
 
 
00ed5fb
394915e
 
72f13c3
 
 
 
 
 
 
394915e
40b3207
b64c76c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
394915e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40b3207
394915e
 
00ed5fb
394915e
 
 
 
 
 
 
 
 
 
 
 
 
00ed5fb
394915e
 
 
00ed5fb
 
 
394915e
 
 
 
00ed5fb
40b3207
394915e
 
 
 
 
 
 
 
 
 
00ed5fb
 
394915e
b64c76c
394915e
 
 
 
00ed5fb
394915e
 
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
import os
import sys
import json
import math
import string
import random
import argparse
import logging
from typing import List, Tuple, Optional, AsyncGenerator

import aiohttp
import uvicorn
import requests
import gradio as gr
from fastapi import FastAPI, Request
from starlette.responses import HTMLResponse

# Configure logging
logging.basicConfig(
    level=logging.INFO,
    format='%(asctime)s [%(levelname)s] %(message)s',
    handlers=[
        logging.StreamHandler(sys.stdout)
    ]
)
logger = logging.getLogger(__name__)

# Environment Variables
API_BASE = os.getenv("API_BASE", "env")
API_KEY = os.getenv("API_KEY")
OAI_API_KEY = os.getenv("OPENAI_API_KEY")
BASE_URL = os.getenv("OPENAI_BASE_URL", "https://api.openai.com/v1")
DEF_MODELS = [
    "chatgpt-4o-latest", "gpt-4-0125-preview", "gpt-4-0613", "gpt-4-1106-preview",
    "gpt-4-turbo-2024-04-09", "gpt-4-turbo-preview", "gpt-4-turbo", "gpt-4",
    "gpt-4o-2024-05-13", "gpt-4o-2024-08-06", "gpt-4o-2024-11-20",
    "gpt-4o-mini-2024-07-18", "gpt-4o-mini", "gpt-4o"
]

models = []
model_list = {}

# Exception for API Key handling
class APIKeyError(Exception):
    pass

def get_api_key(call: str = 'api_key') -> str:
    key = API_KEY if call == 'api_key' else (OAI_API_KEY if call == 'oai_api_key' else API_KEY)
    if ',' in key:
        selected_key = random.choice(key.split(','))
        logger.debug(f"Selected API key: {selected_key}")
        return selected_key
    return key

def encode_chat(messages: List[dict]) -> str:
    encoded = "\n".join(
        f"<|im_start|>{msg['role']}{' [' + msg['name'] + ']' if 'name' in msg else ''}\n{msg['content']}<|end_of_text|>"
        for msg in messages
    )
    logger.debug(f"Encoded chat: {encoded}")
    return encoded

def check_models():
    global BASE_URL, API_KEY
    if API_BASE == "env":
        try:
            response = requests.get(
                f"{BASE_URL}/models",
                headers={"Authorization": f"Bearer {get_api_key()}"}
            )
            response.raise_for_status()
            data = response.json()
            if 'data' not in data:
                logger.warning("No 'data' in response. Falling back to default BASE_URL and API_KEY.")
                BASE_URL = "https://api.openai.com/v1"
                API_KEY = OAI_API_KEY
            else:
                logger.info("Successfully fetched models from API_BASE.")
        except requests.RequestException as e:
            logger.error(f"Error testing API endpoint: {e}. Falling back to default BASE_URL and API_KEY.")
            BASE_URL = "https://api.openai.com/v1"
            API_KEY = OAI_API_KEY
    else:
        BASE_URL = "https://api.openai.com/v1"
        API_KEY = OAI_API_KEY
        logger.info("Using default BASE_URL and OAI_API_KEY.")

def load_models():
    global models, model_list
    models = sorted(DEF_MODELS)
    model_list = {
        "object": "list",
        "data": [{"id": model_id, "object": "model", "created": 0, "owned_by": "system"} for model_id in models]
    }
    logger.info(f"Loaded models: {models}")

def handle_api_keys():
    global API_KEY
    valid_keys = []
    keys = API_KEY.split(',') if ',' in API_KEY else [API_KEY]
    for key in keys:
        try:
            response = requests.get(
                f"{BASE_URL}/models",
                headers={"Authorization": f"Bearer {key.strip()}"}
            )
            response.raise_for_status()
            if 'data' in response.json():
                valid_keys.append(key.strip())
                logger.debug(f"Valid API key: {key.strip()}")
            else:
                logger.warning(f"API key {key.strip()} is invalid.")
        except requests.RequestException as e:
            logger.error(f"API key {key.strip()} is not valid or an error occurred: {e}")

    if not valid_keys:
        raise APIKeyError("No valid API keys are available.")
    API_KEY = ",".join(valid_keys)
    logger.info(f"Using API keys: {API_KEY}")

def moderate(messages: List[dict]) -> Optional[dict]:
    try:
        response = requests.post(
            f"{BASE_URL}/moderations",
            headers={
                "Content-Type": "application/json",
                "Authorization": f"Bearer {get_api_key('api_key')}"
            },
            json={"input": encode_chat(messages)}
        )
        response.raise_for_status()
        moderation_result = response.json()
        logger.debug(f"Moderation result: {moderation_result}")
    except requests.RequestException as e:
        logger.error(f"Moderation request failed: {e}. Trying fallback URL.")
        try:
            response = requests.post(
                "https://api.openai.com/v1/moderations",
                headers={
                    "Content-Type": "application/json",
                    "Authorization": f"Bearer {get_api_key('oai_api_key')}"
                },
                json={"input": encode_chat(messages)}
            )
            response.raise_for_status()
            moderation_result = response.json()
            logger.debug(f"Moderation result from fallback: {moderation_result}")
        except requests.RequestException as ex:
            logger.error(f"Fallback moderation request failed: {ex}")
            return None

    try:
        if isinstance(moderation_result, list):
            flagged = any(result.get("flagged", False) for result in moderation_result)
        else:
            flagged = moderation_result.get("flagged", False)
        if flagged:
            logger.info("Content flagged by moderation.")
            return moderation_result
    except KeyError as e:
        logger.error(f"Key error during moderation processing: {e}")
        return None

    return None

async def stream_chat(params: dict):
    async with aiohttp.ClientSession() as session:
        for attempt, url in enumerate([f"{BASE_URL}/chat/completions", "https://api.openai.com/v1/chat/completions"], start=1):
            try:
                async with session.post(
                    url,
                    headers={
                        "Authorization": f"Bearer {get_api_key('api_key' if attempt == 1 else 'oai_api_key')}",
                        "Content-Type": "application/json"
                    },
                    json=params,
                    timeout=30
                ) as resp:
                    resp.raise_for_status()
                    buffer = ""
                    async for chunk in resp.content:
                        if chunk:
                            buffer += chunk.decode('utf-8')
                            while '\n' in buffer:
                                line, buffer = buffer.split('\n', 1)
                                line = line.strip()
                                if line.startswith("data: "):
                                    line = line[6:].strip()
                                if line == "[DONE]":
                                    return
                                if not line:
                                    continue
                                try:
                                    message = json.loads(line)
                                    yield message
                                except json.JSONDecodeError:
                                    continue
                break
            except aiohttp.ClientError as e:
                logger.error(f"Stream chat request failed on attempt {attempt}: {e}")
                if attempt == 2:
                    return

def rnd(length: int = 8) -> str:
    result = ''.join(random.choices(string.ascii_letters + string.digits, k=length))
    logger.debug(f"Generated random string: {result}")
    return result

async def respond(
    message: str,
    history: List[Tuple[str, str]],
    model_name: str,
    max_tokens: int,
    temperature: float,
    top_p: float,
) -> AsyncGenerator[str, None]:
    messages = []
    for user_msg, assistant_msg in history:
        if user_msg:
            messages.append({"role": "user", "content": user_msg})
        if assistant_msg:
            messages.append({"role": "assistant", "content": assistant_msg})

    if message:
        messages.append({"role": "user", "content": message})
        moderation = moderate(messages)
        if moderation:
            reasons = []
            categories = moderation[0].get('categories', {}) if isinstance(moderation, list) else moderation.get('categories', {})
            for category, flagged in categories.items():
                if flagged:
                    reasons.append(category)
            if reasons:
                response = "[MODERATION] I'm sorry, but I can't assist with that.\n\nReasons:\n```\n" + "\n".join(f"{i+1}. {reason}" for i, reason in enumerate(reasons)) + "\n```"
            else:
                response = "[MODERATION] I'm sorry, but I can't assist with that."
            logger.info("Message flagged by moderation.")
            yield response
            return

    params = {
        "model": model_name,
        "messages": messages,
        "max_tokens": max_tokens,
        "temperature": temperature,
        "top_p": top_p,
        "user": rnd(),
        "stream": True
    }

    try:
        response_text = ""
        async for token in stream_chat(params):
            if token and 'choices' in token and len(token['choices']) > 0:
                delta = token['choices'][0].get('delta', {})
                content = delta.get("content", delta.get("refusal", ""))
                response_text += content
                yield response_text
        
        if not response_text:
            yield "I apologize, but I was unable to generate a response. Please try again."
            
    except Exception as e:
        logger.error(f"Error during chat response generation: {e}")
        yield "I encountered an error while processing your request. Please try again later."

def create_gradio_interface() -> gr.ChatInterface:
    return gr.ChatInterface(
        respond,
        title="gpt-4o-mini",
        description="The chat is back online for a not-so-long time.",
        additional_inputs=[
            gr.Dropdown(choices=models, value="gpt-4o-mini", label="Model"),
            gr.Slider(minimum=1, maximum=4096, value=4096, step=1, label="Max new tokens"),
            gr.Slider(minimum=0.1, maximum=2.0, value=0.7, step=0.05, label="Temperature"),
            gr.Slider(minimum=0.05, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)"),
        ],
        css="footer{display:none !important}",
        head="""<script>
            if(!confirm("By using our application, which integrates with OpenAI's API, you acknowledge and agree to the following terms regarding the data you provide:\\n\\n1. Data Collection: This application may log the following data through the Gradio endpoint or the API endpoint: message requests (including messages, responses, model settings, and images sent along with the messages), images that were generated (including only the prompt and the image), search tool calls (including query, search results, summaries, and output responses), and moderation checks (including input and output).\\n2. Data Retention and Removal: Data is retained until further notice or until a specific request for removal is made.\\n3. Data Usage: The collected data may be used for various purposes, including but not limited to, administrative review of logs, AI training, and publication as a dataset.\\n4. Privacy: Please avoid sharing any personal information.\\n\\nBy continuing to use our application, you explicitly consent to the collection, use, and potential sharing of your data as described above. If you disagree with our data collection, usage, and sharing practices, we advise you not to use our application.")) location.href="/declined";
        </script>"""
    )

def create_fastapi_app() -> FastAPI:
    app = FastAPI()

    @app.get("/declined")
    def declined():
        return HTMLResponse(content="""
            <html>
                <head>
                    <title>Declined</title>
                </head>
                <body>
                    <p>Ok, you can go back to Hugging Face. I just didn't have any idea how to handle decline so you are redirected here.</p><br/>
                    <a href="/">Go back</a>
                </body>
            </html>
        """)

    gradio_app = create_gradio_interface()
    app = gr.mount_gradio_app(app, gradio_app, path="/")
    return app

class ArgParser(argparse.ArgumentParser):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.add_argument("-s", "--server", type=str, default="0.0.0.0", help="Server host.")
        self.add_argument("-p", "--port", type=int, default=7860, help="Server port.")
        self.add_argument("-d", "--dev", action="store_true", help="Run in development mode.")
        self.args = self.parse_args(sys.argv[1:])

def main():
    try:
        handle_api_keys()
        load_models()
        check_models()
    except APIKeyError as e:
        logger.critical(e)
        sys.exit(1)

    app = create_fastapi_app()
    args = ArgParser().args

    uvicorn.run(
        app,
        host=args.server,
        port=args.port,
        reload=args.dev
    )

if __name__ == "__main__":
    main()