Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,258 +1,42 @@
|
|
1 |
-
|
2 |
-
import os
|
3 |
-
import uuid
|
4 |
-
import requests
|
5 |
-
import gradio as gr
|
6 |
-
import modelscope_studio.components.antd as antd
|
7 |
-
import modelscope_studio.components.antdx as antdx
|
8 |
-
import modelscope_studio.components.base as ms
|
9 |
-
from openai import OpenAI
|
10 |
|
11 |
-
|
12 |
-
try:
|
13 |
-
response = requests.get("https://raw.githubusercontent.com/ALPERALL/AlpDroid/refs/heads/main/prompt.txt")
|
14 |
-
return response.text.strip() if response.status_code == 200 else """Sen gelişmiş, çok yönlü, anlayışlı ve kullanıcıya destek olan bir yapay zeka asistanısın."""
|
15 |
-
except Exception as e:
|
16 |
-
print(f"Sistem prompt'u yüklenirken hata: {e}")
|
17 |
-
return """Sen gelişmiş, çok yönlü, anlayışlı ve kullanıcıya destek olan bir yapay zeka asistanısın."""
|
18 |
|
19 |
-
#
|
20 |
-
SYSTEM_PROMPT = fetch_system_prompt()
|
21 |
-
MODEL = "alpDroid-32b"
|
22 |
-
SAVE_HISTORY = True
|
23 |
-
IS_MODELSCOPE_STUDIO = os.getenv('MODELSCOPE_ENVIRONMENT') == 'studio'
|
24 |
-
|
25 |
-
# Logo ve görsel yolu
|
26 |
-
LOGO_PATH = os.path.join(os.path.dirname(__file__), "alpDroid_logo.png")
|
27 |
-
|
28 |
-
# Varsayılan Komut İstemleri
|
29 |
DEFAULT_PROMPTS = [{
|
30 |
-
"category": "
|
31 |
"prompts": [
|
32 |
-
"
|
33 |
-
"
|
34 |
-
"
|
35 |
]
|
36 |
}, {
|
37 |
-
"category": "✍️
|
38 |
"prompts": [
|
39 |
-
"
|
40 |
-
"
|
41 |
-
"
|
42 |
]
|
43 |
}]
|
44 |
|
45 |
-
#
|
46 |
DEFAULT_SUGGESTIONS = [{
|
47 |
-
"label":
|
48 |
-
"value": "Strateji",
|
49 |
"children": [
|
50 |
-
{"label": "İş Planı", "value": "
|
51 |
-
{"label": "
|
52 |
-
{"label": "
|
53 |
]
|
54 |
}, {
|
55 |
-
"label":
|
56 |
-
"value": "Yazma",
|
57 |
"children": [
|
58 |
-
{"label": "
|
59 |
-
{"label": "
|
60 |
-
{"label": "
|
61 |
]
|
62 |
}]
|
63 |
|
64 |
-
|
65 |
-
DEFAULT_LOCALE = 'tr_TR'
|
66 |
-
DEFAULT_THEME = {"token": {"colorPrimary": "#6A57FF"}}
|
67 |
-
|
68 |
-
# CSS Stilleri
|
69 |
-
css = """
|
70 |
-
.gradio-container {
|
71 |
-
padding: 0 !important;
|
72 |
-
}
|
73 |
-
|
74 |
-
.gradio-container > main.fillable {
|
75 |
-
padding: 0 !important;
|
76 |
-
}
|
77 |
-
|
78 |
-
#chatbot {
|
79 |
-
height: calc(100vh - 21px - 16px);
|
80 |
-
}
|
81 |
-
|
82 |
-
#chatbot .chatbot-conversations {
|
83 |
-
height: 100%;
|
84 |
-
background-color: var(--ms-gr-ant-color-bg-layout);
|
85 |
-
}
|
86 |
-
|
87 |
-
#chatbot .chatbot-conversations .chatbot-conversations-list {
|
88 |
-
padding-left: 0;
|
89 |
-
padding-right: 0;
|
90 |
-
}
|
91 |
-
|
92 |
-
#chatbot .chatbot-chat {
|
93 |
-
padding: 32px;
|
94 |
-
height: 100%;
|
95 |
-
}
|
96 |
-
"""
|
97 |
-
|
98 |
-
class Gradio_Events:
|
99 |
-
@staticmethod
|
100 |
-
def format_history(history):
|
101 |
-
messages = [{
|
102 |
-
"role": "system",
|
103 |
-
"content": SYSTEM_PROMPT,
|
104 |
-
}]
|
105 |
-
for item in history:
|
106 |
-
if item["role"] == "user":
|
107 |
-
messages.append({"role": "user", "content": item["content"]})
|
108 |
-
elif item["role"] == "assistant":
|
109 |
-
messages.append({"role": "assistant", "content": item["content"]})
|
110 |
-
return messages
|
111 |
-
|
112 |
-
@staticmethod
|
113 |
-
def _submit(state_value):
|
114 |
-
history = state_value["conversations_history"][state_value["conversation_id"]]
|
115 |
-
history_messages = Gradio_Events.format_history(history)
|
116 |
-
|
117 |
-
client = OpenAI(
|
118 |
-
base_url="https://dashscope.aliyuncs.com/compatible-mode/v1",
|
119 |
-
api_key=os.getenv("ALPDROIDS_API_KEY"),
|
120 |
-
)
|
121 |
-
|
122 |
-
history.append({
|
123 |
-
"role": "assistant",
|
124 |
-
"content": "",
|
125 |
-
"key": str(uuid.uuid4()),
|
126 |
-
"meta": {"reason_content": ""},
|
127 |
-
"loading": True,
|
128 |
-
})
|
129 |
-
|
130 |
-
yield {
|
131 |
-
"chatbot": gr.update(items=history),
|
132 |
-
"state": gr.update(value=state_value),
|
133 |
-
}
|
134 |
-
|
135 |
-
try:
|
136 |
-
response = client.chat.completions.create(
|
137 |
-
model=MODEL,
|
138 |
-
messages=history_messages,
|
139 |
-
stream=True
|
140 |
-
)
|
141 |
-
|
142 |
-
thought_done = False
|
143 |
-
for chunk in response:
|
144 |
-
content = chunk.choices[0].delta.content or ""
|
145 |
-
history[-1]["loading"] = False
|
146 |
-
|
147 |
-
if content and not thought_done:
|
148 |
-
thought_done = True
|
149 |
-
history[-1]["meta"]["reason_content"] = history[-1]["content"]
|
150 |
-
history[-1]["content"] = ""
|
151 |
-
history[-1]["meta"]["thought_end_message"] = "Düşünme tamamlandı"
|
152 |
-
|
153 |
-
if not thought_done:
|
154 |
-
history[-1]["content"] += content
|
155 |
-
else:
|
156 |
-
history[-1]["content"] += content
|
157 |
-
|
158 |
-
yield {
|
159 |
-
"chatbot": gr.update(items=history),
|
160 |
-
"state": gr.update(value=state_value)
|
161 |
-
}
|
162 |
-
|
163 |
-
history[-1]["meta"]["end"] = True
|
164 |
-
|
165 |
-
yield {
|
166 |
-
"chatbot": gr.update(items=history),
|
167 |
-
"state": gr.update(value=state_value),
|
168 |
-
}
|
169 |
-
except Exception as e:
|
170 |
-
history[-1]["loading"] = False
|
171 |
-
history[-1]["meta"]["end"] = True
|
172 |
-
history[-1]["meta"]["error"] = True
|
173 |
-
history[-1]["content"] = "Yanıt oluşturulurken hata meydana geldi."
|
174 |
-
|
175 |
-
yield {
|
176 |
-
"chatbot": gr.update(items=history),
|
177 |
-
"state": gr.update(value=state_value)
|
178 |
-
}
|
179 |
-
print('Hata: ', e)
|
180 |
-
raise e
|
181 |
-
|
182 |
-
@staticmethod
|
183 |
-
def submit(sender_value, state_value):
|
184 |
-
if not state_value["conversation_id"]:
|
185 |
-
random_id = str(uuid.uuid4())
|
186 |
-
history = []
|
187 |
-
state_value["conversation_id"] = random_id
|
188 |
-
state_value["conversations_history"][random_id] = history
|
189 |
-
state_value["conversations"].append({
|
190 |
-
"label": sender_value,
|
191 |
-
"key": random_id
|
192 |
-
})
|
193 |
-
|
194 |
-
history = state_value["conversations_history"][state_value["conversation_id"]]
|
195 |
-
history.append({
|
196 |
-
"role": "user",
|
197 |
-
"meta": {},
|
198 |
-
"key": str(uuid.uuid4()),
|
199 |
-
"content": sender_value
|
200 |
-
})
|
201 |
-
|
202 |
-
yield Gradio_Events.preprocess_submit()(state_value)
|
203 |
-
try:
|
204 |
-
for chunk in Gradio_Events._submit(state_value):
|
205 |
-
yield chunk
|
206 |
-
except Exception as e:
|
207 |
-
raise e
|
208 |
-
finally:
|
209 |
-
yield Gradio_Events.postprocess_submit(state_value)
|
210 |
-
|
211 |
-
# Diğer statik metodlar (regenerate_message, preprocess_submit, vb.) buraya eklenecek
|
212 |
-
# Kod çok uzun olduğu için tamamını göstermiyorum
|
213 |
-
|
214 |
-
@staticmethod
|
215 |
-
def preprocess_submit(clear_input=True):
|
216 |
-
def preprocess_submit_handler(state_value):
|
217 |
-
history = state_value["conversations_history"][state_value["conversation_id"]]
|
218 |
-
for conversation in history:
|
219 |
-
if "meta" in conversation:
|
220 |
-
conversation["meta"]["disabled"] = True
|
221 |
-
return {
|
222 |
-
"sender": gr.update(value=None, loading=True) if clear_input else gr.update(loading=True),
|
223 |
-
"conversations": gr.update(active_key=state_value["conversation_id"], items=list(
|
224 |
-
map(lambda item: {**item, "disabled": True if item["key"] != state_value["conversation_id"] else False},
|
225 |
-
state_value["conversations"]))),
|
226 |
-
"add_conversation_btn": gr.update(disabled=True),
|
227 |
-
"clear_btn": gr.update(disabled=True),
|
228 |
-
"conversation_delete_menu_item": gr.update(disabled=True),
|
229 |
-
"chatbot": gr.update(items=history),
|
230 |
-
"state": gr.update(value=state_value),
|
231 |
-
}
|
232 |
-
return preprocess_submit_handler
|
233 |
-
|
234 |
-
@staticmethod
|
235 |
-
def postprocess_submit(state_value):
|
236 |
-
history = state_value["conversations_history"][state_value["conversation_id"]]
|
237 |
-
for conversation in history:
|
238 |
-
if "meta" in conversation:
|
239 |
-
conversation["meta"]["disabled"] = False
|
240 |
-
return {
|
241 |
-
"sender": gr.update(loading=False),
|
242 |
-
"conversation_delete_menu_item": gr.update(disabled=False),
|
243 |
-
"clear_btn": gr.update(disabled=False),
|
244 |
-
"conversations": gr.update(items=state_value["conversations"]),
|
245 |
-
"add_conversation_btn": gr.update(disabled=False),
|
246 |
-
"chatbot": gr.update(items=history),
|
247 |
-
"state": gr.update(value=state_value),
|
248 |
-
}
|
249 |
-
|
250 |
-
def logo():
|
251 |
-
with antd.Typography.Title(level=1, elem_style=dict(fontSize=24, padding=8, margin=0)):
|
252 |
-
with antd.Flex(align="center", gap="small", justify="center"):
|
253 |
-
antd.Image(LOGO_PATH, preview=False, alt="logo", width=24, height=24)
|
254 |
-
ms.Span("AlpDroid-32B")
|
255 |
-
|
256 |
with gr.Blocks(css=css, fill_width=True) as demo:
|
257 |
state = gr.State({
|
258 |
"conversations_history": {},
|
@@ -261,34 +45,61 @@ with gr.Blocks(css=css, fill_width=True) as demo:
|
|
261 |
"editing_message_index": -1,
|
262 |
})
|
263 |
|
264 |
-
with ms.Application(), antdx.XProvider(
|
265 |
-
|
266 |
-
|
267 |
-
|
268 |
-
|
269 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
270 |
|
271 |
-
#
|
272 |
-
with
|
273 |
-
ms.
|
274 |
-
with ms.Slot("
|
275 |
-
antd.Icon("
|
276 |
-
|
277 |
-
|
278 |
-
|
279 |
-
|
280 |
-
|
281 |
-
|
282 |
-
|
283 |
-
|
284 |
-
|
285 |
-
|
286 |
-
|
287 |
-
|
288 |
-
|
289 |
-
|
290 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
291 |
|
292 |
if __name__ == "__main__":
|
293 |
-
demo.
|
294 |
-
</artifact>
|
|
|
1 |
+
# ... (Önceki import ve config kısımları aynı)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
|
3 |
+
alp_logo = os.path.join(os.path.dirname(__file__), "alp.png")
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
|
5 |
+
# TÜRKÇE PROMPTLAR
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
DEFAULT_PROMPTS = [{
|
7 |
+
"category": "📝 Plan Yap",
|
8 |
"prompts": [
|
9 |
+
"İş kurma planı oluştur",
|
10 |
+
"Hedeflerim için yol haritası hazırla",
|
11 |
+
"Mülakat provası için strateji öner"
|
12 |
]
|
13 |
}, {
|
14 |
+
"category": "✍️ İçerik Üret",
|
15 |
"prompts": [
|
16 |
+
"Teknoloji blogu için makale taslağı oluştur",
|
17 |
+
"Şirket vizyon metni yaz",
|
18 |
+
"Satış e-postası taslağı hazırla"
|
19 |
]
|
20 |
}]
|
21 |
|
22 |
+
# TÜRKÇE ÖNERİLER
|
23 |
DEFAULT_SUGGESTIONS = [{
|
24 |
+
"label": 'Planlama',
|
|
|
25 |
"children": [
|
26 |
+
{"label": "İş Planı", "value": "İş kurma planı oluştur"},
|
27 |
+
{"label": "Hedef Planı", "value": "Hedeflerim için yol haritası hazırla"},
|
28 |
+
{"label": "Mülakat Stratejisi", "value": "Mülakat provası için strateji öner"}
|
29 |
]
|
30 |
}, {
|
31 |
+
"label": 'Yazım',
|
|
|
32 |
"children": [
|
33 |
+
{"label": "Teknoloji Makalesi", "value": "Teknoloji blogu için makale taslağı oluştur"},
|
34 |
+
{"label": "Vizyon Metni", "value": "Şirket vizyon metni yaz"},
|
35 |
+
{"label": "Satış E-postası", "value": "Satış e-postası taslağı hazırla"}
|
36 |
]
|
37 |
}]
|
38 |
|
39 |
+
# TÜRKÇE ARAYÜZ KODU
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
40 |
with gr.Blocks(css=css, fill_width=True) as demo:
|
41 |
state = gr.State({
|
42 |
"conversations_history": {},
|
|
|
45 |
"editing_message_index": -1,
|
46 |
})
|
47 |
|
48 |
+
with ms.Application(), antdx.XProvider(
|
49 |
+
theme=DEFAULT_THEME, locale='tr_TR'), ms.AutoLoading():
|
50 |
+
|
51 |
+
# SOL PANEL
|
52 |
+
with antd.Col(md=dict(flex="0 0 260px"), elem_classes="chatbot-conversations"):
|
53 |
+
with antd.Flex(vertical=True, gap="small"):
|
54 |
+
# Logo ve Başlık
|
55 |
+
with antd.Typography.Title(level=1, style={"fontSize": 24, "margin": 0}):
|
56 |
+
with antd.Flex(align="center", gap="small"):
|
57 |
+
antd.Image(alp_logo, width=32, height=32, preview=False)
|
58 |
+
ms.Span("AlpDroid AI")
|
59 |
+
|
60 |
+
# Yeni Sohbet Butonu
|
61 |
+
with antd.Button("Yeni Sohbet", block=True, icon=antd.Icon("PlusOutlined")) as add_conversation_btn
|
62 |
+
|
63 |
+
# Sohbet Geçmişi
|
64 |
+
with antdx.Conversations() as conversations:
|
65 |
+
with ms.Slot('menu.items'):
|
66 |
+
antd.Menu.Item("Sil", key="delete", danger=True, icon=antd.Icon("DeleteOutlined"))
|
67 |
+
|
68 |
+
# SAĞ PANEL
|
69 |
+
with antd.Col(flex=1, style={"height": "100%"}):
|
70 |
+
with antd.Flex(vertical=True, gap="middle"):
|
71 |
+
# Sohbet Kutusu
|
72 |
+
with antdx.Bubble.List(items=DEFAULT_CONVERSATIONS_HISTORY) as chatbot:
|
73 |
|
74 |
+
# Kullanıcı Mesajı
|
75 |
+
with antdx.Bubble.List.Role(role="user", placement="end"):
|
76 |
+
ms.Markdown()
|
77 |
+
with ms.Slot("footer"):
|
78 |
+
antd.Button(icon=antd.Icon("EditOutlined"))
|
79 |
+
antd.Popconfirm("Bu mesajı silmek istiyor musunuz?",
|
80 |
+
ok_button_props={"danger": True},
|
81 |
+
children=[antd.Button(icon=antd.Icon("DeleteOutlined"))]
|
82 |
+
)
|
83 |
+
|
84 |
+
# Asistan Mesajı
|
85 |
+
with antdx.Bubble.List.Role(role="assistant", placement="start"):
|
86 |
+
antd.Avatar(alp_logo)
|
87 |
+
ms.Markdown()
|
88 |
+
with ms.Slot("footer"):
|
89 |
+
antd.Button(icon=antd.Icon("SyncOutlined"))
|
90 |
+
antd.Popconfirm("Mesajı yeniden oluştur?",
|
91 |
+
children=[antd.Button(icon=antd.Icon("DeleteOutlined"))]
|
92 |
+
)
|
93 |
+
|
94 |
+
# Giriş Alanı
|
95 |
+
with antdx.Sender(placeholder="Mesaj yazın veya '/' ile komut seçin"):
|
96 |
+
with antd.Tooltip("Geçmişi Temizle"):
|
97 |
+
antd.Button(icon=antd.Icon("ClearOutlined"))
|
98 |
+
with antdx.Suggestion(items=DEFAULT_SUGGESTIONS):
|
99 |
+
antd.Input.Textarea()
|
100 |
+
|
101 |
+
# EVENT HANDLERS (Önceki koddaki olay yönetim fonksiyonları aynen kalacak)
|
102 |
+
# ... (Önceki event handler'lar buraya gelecek)
|
103 |
|
104 |
if __name__ == "__main__":
|
105 |
+
demo.launch()
|
|