Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,5 +1,4 @@
|
|
| 1 |
import os
|
| 2 |
-
os.system('pip install dashscope')
|
| 3 |
import gradio as gr
|
| 4 |
from http import HTTPStatus
|
| 5 |
import dashscope
|
|
@@ -7,14 +6,18 @@ from dashscope import Generation
|
|
| 7 |
from dashscope.api_entities.dashscope_response import Role
|
| 8 |
from typing import List, Optional, Tuple, Dict
|
| 9 |
from urllib.error import HTTPError
|
| 10 |
-
default_system = 'You are Qwen, created by Said. You are a best helpful developer.'
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
| 14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
History = List[Tuple[str, str]]
|
| 16 |
Messages = List[Dict[str, str]]
|
| 17 |
|
|
|
|
| 18 |
def clear_session() -> History:
|
| 19 |
return '', []
|
| 20 |
|
|
@@ -30,7 +33,6 @@ def history_to_messages(history: History, system: str) -> Messages:
|
|
| 30 |
messages.append({'role': Role.ASSISTANT, 'content': h[1]})
|
| 31 |
return messages
|
| 32 |
|
| 33 |
-
|
| 34 |
def messages_to_history(messages: Messages) -> Tuple[str, History]:
|
| 35 |
assert messages[0]['role'] == Role.SYSTEM
|
| 36 |
system = messages[0]['content']
|
|
@@ -39,7 +41,6 @@ def messages_to_history(messages: Messages) -> Tuple[str, History]:
|
|
| 39 |
history.append([q['content'], r['content']])
|
| 40 |
return system, history
|
| 41 |
|
| 42 |
-
|
| 43 |
def model_chat(query: Optional[str], history: Optional[History], system: str
|
| 44 |
) -> Tuple[str, str, History]:
|
| 45 |
if query is None:
|
|
@@ -49,7 +50,7 @@ def model_chat(query: Optional[str], history: Optional[History], system: str
|
|
| 49 |
messages = history_to_messages(history, system)
|
| 50 |
messages.append({'role': Role.USER, 'content': query})
|
| 51 |
gen = Generation.call(
|
| 52 |
-
model
|
| 53 |
messages=messages,
|
| 54 |
result_format='message',
|
| 55 |
stream=True
|
|
@@ -66,7 +67,7 @@ def model_chat(query: Optional[str], history: Optional[History], system: str
|
|
| 66 |
response.code, response.message
|
| 67 |
))
|
| 68 |
|
| 69 |
-
|
| 70 |
with gr.Blocks() as demo:
|
| 71 |
gr.Markdown("""<center><font size=8>Qwen2.5-Coder-32B-Instruct Bot👾</center>""")
|
| 72 |
|
|
@@ -86,7 +87,7 @@ with gr.Blocks() as demo:
|
|
| 86 |
sumbit.click(model_chat,
|
| 87 |
inputs=[textbox, chatbot, system_state],
|
| 88 |
outputs=[textbox, chatbot, system_input],
|
| 89 |
-
concurrency_limit
|
| 90 |
clear_history.click(fn=clear_session,
|
| 91 |
inputs=[],
|
| 92 |
outputs=[textbox, chatbot])
|
|
@@ -95,4 +96,4 @@ with gr.Blocks() as demo:
|
|
| 95 |
outputs=[system_state, system_input, chatbot])
|
| 96 |
|
| 97 |
demo.queue(api_open=False)
|
| 98 |
-
demo.launch(max_threads=
|
|
|
|
| 1 |
import os
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
from http import HTTPStatus
|
| 4 |
import dashscope
|
|
|
|
| 6 |
from dashscope.api_entities.dashscope_response import Role
|
| 7 |
from typing import List, Optional, Tuple, Dict
|
| 8 |
from urllib.error import HTTPError
|
|
|
|
| 9 |
|
| 10 |
+
# متغیر پیشفرض سیستم
|
| 11 |
+
default_system = 'You are Qwen, created by Alibaba Cloud. You are a helpful assistant.'
|
| 12 |
|
| 13 |
+
# تنظیم کلید API از متغیر محیطی
|
| 14 |
+
dashscope.api_key = os.getenv('YOUR_API_TOKEN') # Ensure it's set securely in your environment
|
| 15 |
+
|
| 16 |
+
# تعریف نوع دادهها
|
| 17 |
History = List[Tuple[str, str]]
|
| 18 |
Messages = List[Dict[str, str]]
|
| 19 |
|
| 20 |
+
# توابع کمکی
|
| 21 |
def clear_session() -> History:
|
| 22 |
return '', []
|
| 23 |
|
|
|
|
| 33 |
messages.append({'role': Role.ASSISTANT, 'content': h[1]})
|
| 34 |
return messages
|
| 35 |
|
|
|
|
| 36 |
def messages_to_history(messages: Messages) -> Tuple[str, History]:
|
| 37 |
assert messages[0]['role'] == Role.SYSTEM
|
| 38 |
system = messages[0]['content']
|
|
|
|
| 41 |
history.append([q['content'], r['content']])
|
| 42 |
return system, history
|
| 43 |
|
|
|
|
| 44 |
def model_chat(query: Optional[str], history: Optional[History], system: str
|
| 45 |
) -> Tuple[str, str, History]:
|
| 46 |
if query is None:
|
|
|
|
| 50 |
messages = history_to_messages(history, system)
|
| 51 |
messages.append({'role': Role.USER, 'content': query})
|
| 52 |
gen = Generation.call(
|
| 53 |
+
model="qwen2.5-coder-32b-instruct",
|
| 54 |
messages=messages,
|
| 55 |
result_format='message',
|
| 56 |
stream=True
|
|
|
|
| 67 |
response.code, response.message
|
| 68 |
))
|
| 69 |
|
| 70 |
+
# ساخت رابط Gradio
|
| 71 |
with gr.Blocks() as demo:
|
| 72 |
gr.Markdown("""<center><font size=8>Qwen2.5-Coder-32B-Instruct Bot👾</center>""")
|
| 73 |
|
|
|
|
| 87 |
sumbit.click(model_chat,
|
| 88 |
inputs=[textbox, chatbot, system_state],
|
| 89 |
outputs=[textbox, chatbot, system_input],
|
| 90 |
+
concurrency_limit=10) # تنظیم محدودیت به 10
|
| 91 |
clear_history.click(fn=clear_session,
|
| 92 |
inputs=[],
|
| 93 |
outputs=[textbox, chatbot])
|
|
|
|
| 96 |
outputs=[system_state, system_input, chatbot])
|
| 97 |
|
| 98 |
demo.queue(api_open=False)
|
| 99 |
+
demo.launch(max_threads=10) # تنظیم تعداد رشتهها به 10
|