File size: 8,700 Bytes
29232b4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5456854
 
 
 
 
 
 
60f9e8e
 
 
 
 
 
 
 
 
5456854
7d6f26e
 
5456854
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
60f9e8e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5456854
 
 
 
 
b58bc4a
5456854
 
 
 
 
 
7d6f26e
5456854
 
 
 
 
 
 
 
 
 
 
60f9e8e
7d6f26e
 
5456854
7d6f26e
 
5456854
 
 
7d6f26e
 
 
 
 
 
 
 
 
 
 
5456854
 
 
60f9e8e
 
 
 
 
5456854
 
 
 
60f9e8e
5456854
 
 
 
60f9e8e
5456854
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
60f9e8e
 
5456854
 
 
 
 
3d1964a
5456854
cf13d53
5456854
 
 
 
 
60f9e8e
 
 
5456854
60f9e8e
 
5456854
60f9e8e
5456854
60f9e8e
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
#
# Copyright (C) Hadad <[email protected]>
# All rights reserved.
#
# This code is made available under the Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0) License.
# You are free to share and adapt the code for non-commercial purposes, as long as you provide appropriate credit,
# do not use it for commercial purposes, and distribute your contributions under the same license.
#
# Contributions can be made by directly submitting pull requests.
#
# For inquiries or permission requests, please contact [email protected].
#
# License: Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0)
#

import gradio as gr
import requests
import json
import os
import threading
import random
import time
import pytesseract
import pdfplumber
import docx
import pandas as pd
import pptx
import fitz
import io
from pathlib import Path
from PIL import Image

LINUX_SERVER_HOSTS = [host for host in json.loads(os.getenv("LINUX_SERVER_HOST", "[]")) if host]
LINUX_SERVER_PROVIDER_KEYS = [key for key in json.loads(os.getenv("LINUX_SERVER_PROVIDER_KEY", "[]")) if key]

AI_TYPES = {f"AI_TYPE_{i}": os.getenv(f"AI_TYPE_{i}") for i in range(1, 6)}
RESPONSES = {f"RESPONSE_{i}": os.getenv(f"RESPONSE_{i}") for i in range(1, 10)}

MODEL_MAPPING = json.loads(os.getenv("MODEL_MAPPING", "{}"))
MODEL_CONFIG = json.loads(os.getenv("MODEL_CONFIG", "{}"))
MODEL_CHOICES = list(MODEL_MAPPING.values())
DEFAULT_CONFIG = json.loads(os.getenv("DEFAULT_CONFIG", "{}"))

META_TAGS = os.getenv("META_TAGS")

stop_event = threading.Event()
session = requests.Session()

def get_model_key(display_name):
    return next((k for k, v in MODEL_MAPPING.items() if v == display_name), MODEL_CHOICES[0])

def extract_text(file_path):
    ext = Path(file_path).suffix.lower()

    if ext == ".txt":
        try:
            with open(file_path, "r", encoding="utf-8") as file:
                return file.read()
        except:
            return ""

    elif ext == ".pdf":
        text = []
        try:
            with pdfplumber.open(file_path) as pdf:
                for page in pdf.pages:
                    text.append(page.extract_text() or "")
            if not "".join(text).strip():
                text = extract_text_from_pdf_images(file_path)
        except:
            return ""
        return "\n".join(text)

    elif ext in [".doc", ".docx"]:
        try:
            doc = docx.Document(file_path)
            text = "\n".join([para.text for para in doc.paragraphs])
            if not text.strip():
                text = extract_text_from_doc_images(file_path)
            return text
        except:
            return ""

    elif ext in [".xls", ".xlsx"]:
        try:
            df = pd.read_excel(file_path)
            return df.to_string()
        except:
            return ""

    elif ext in [".ppt", ".pptx"]:
        try:
            prs = pptx.Presentation(file_path)
            text = []
            for slide in prs.slides:
                for shape in slide.shapes:
                    if hasattr(shape, "text"):
                        text.append(shape.text)
            return "\n".join(text)
        except:
            return ""

    return ""

def extract_text_from_pdf_images(pdf_path):
    text = []
    try:
        doc = fitz.open(pdf_path)
        for page_num in range(len(doc)):
            pix = doc[page_num].get_pixmap()
            img = Image.frombytes("RGB", [pix.width, pix.height], pix.samples)
            text.append(pytesseract.image_to_string(img))
    except:
        return []
    return text

def extract_text_from_doc_images(doc_path):
    text = []
    try:
        doc = docx.Document(doc_path)
        for rel in doc.part.rels:
            if "image" in doc.part.rels[rel].target_ref:
                img_data = doc.part.rels[rel].target_part.blob
                img = Image.open(io.BytesIO(img_data))
                text.append(pytesseract.image_to_string(img))
    except:
        return []
    return "\n".join(text)

def simulate_streaming_response(text):
    for line in text.splitlines():
        if stop_event.is_set():
            return
        yield line + "\n"
        time.sleep(0.05)

def chat_with_model(history, user_input, selected_model_display):
    if stop_event.is_set():
        yield RESPONSES["RESPONSE_1"]
        return

    if not LINUX_SERVER_PROVIDER_KEYS or not LINUX_SERVER_HOSTS:
        yield RESPONSES["RESPONSE_3"]
        return

    selected_model = get_model_key(selected_model_display)
    model_config = MODEL_CONFIG.get(selected_model, DEFAULT_CONFIG)

    messages = [{"role": "user", "content": user} for user, _ in history]
    messages += [{"role": "assistant", "content": assistant} for _, assistant in history if assistant]
    messages.append({"role": "user", "content": user_input})

    data = {"model": selected_model, "messages": messages, **model_config}

    random.shuffle(LINUX_SERVER_PROVIDER_KEYS)
    random.shuffle(LINUX_SERVER_HOSTS)

    for api_key in LINUX_SERVER_PROVIDER_KEYS[:2]:
        for host in LINUX_SERVER_HOSTS[:2]:
            if stop_event.is_set():
                yield RESPONSES["RESPONSE_1"]
                return
            try:
                response = session.post(host, json=data, headers={"Authorization": f"Bearer {api_key}"})
                if stop_event.is_set():
                    yield RESPONSES["RESPONSE_1"]
                    return
                if response.status_code < 400:
                    ai_text = response.json().get("choices", [{}])[0].get("message", {}).get("content", RESPONSES["RESPONSE_2"])
                    yield from simulate_streaming_response(ai_text)
                    return
            except requests.exceptions.RequestException:
                continue

    yield RESPONSES["RESPONSE_3"]

def respond(user_input, file_path, history, selected_model_display):
    file_text = extract_text(file_path) if file_path else ""
    combined_input = f"{user_input}\n\n{file_text}".strip()

    if not combined_input:
        yield history, gr.update(value=""), gr.update(visible=False, interactive=False), gr.update(visible=True)
        return

    stop_event.clear()
    history.append([combined_input, RESPONSES["RESPONSE_8"]])

    yield history, gr.update(value=""), gr.update(visible=False), gr.update(visible=True)

    ai_response = ""
    for chunk in chat_with_model(history, combined_input, selected_model_display):
        if stop_event.is_set():
            history[-1][1] = RESPONSES["RESPONSE_1"]
            yield history, gr.update(value=""), gr.update(visible=True), gr.update(visible=False)
            return
        ai_response += chunk
        history[-1][1] = ai_response
        yield history, gr.update(value=""), gr.update(visible=False), gr.update(visible=True)

    yield history, gr.update(value=""), gr.update(visible=True), gr.update(visible=False)

def stop_response():
    stop_event.set()
    session.close()

def change_model(new_model_display):
    return [], new_model_display

def check_send_button_enabled(msg, file):
    return gr.update(visible=bool(msg.strip()) or bool(file), interactive=bool(msg.strip()) or bool(file))

with gr.Blocks(fill_height=True, fill_width=True, title=AI_TYPES["AI_TYPE_4"], head=META_TAGS) as demo:
    user_history = gr.State([])
    selected_model = gr.State(MODEL_CHOICES[0])

    chatbot = gr.Chatbot(label=AI_TYPES["AI_TYPE_1"], show_copy_button=True, show_share_button=False, scale=1, elem_id=AI_TYPES["AI_TYPE_2"])
    model_dropdown = gr.Dropdown(label=AI_TYPES["AI_TYPE_3"], show_label=False, choices=MODEL_CHOICES, value=MODEL_CHOICES[0], interactive=True)
    msg = gr.Textbox(label=RESPONSES["RESPONSE_4"], show_label=False, scale=0, placeholder=RESPONSES["RESPONSE_5"])

    with gr.Row():
        send_btn = gr.Button(RESPONSES["RESPONSE_6"], visible=True, interactive=False)
        stop_btn = gr.Button(RESPONSES["RESPONSE_7"], variant=RESPONSES["RESPONSE_9"], visible=False)

    with gr.Accordion("See more...", open=False):
        file_upload = gr.File(label=AI_TYPES["AI_TYPE_5"], file_count="single", type="filepath")

    model_dropdown.change(fn=change_model, inputs=[model_dropdown], outputs=[user_history, selected_model])
    send_btn.click(respond, inputs=[msg, file_upload, user_history, selected_model], outputs=[chatbot, msg, send_btn, stop_btn])
    msg.change(fn=check_send_button_enabled, inputs=[msg, file_upload], outputs=[send_btn])
    stop_btn.click(fn=stop_response, outputs=[send_btn, stop_btn])
    file_upload.change(fn=check_send_button_enabled, inputs=[msg, file_upload], outputs=[send_btn])

demo.launch(show_api=False, max_file_size="1mb")