Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,58 +1,45 @@
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
from ollama import Client
|
|
|
|
| 4 |
|
| 5 |
-
# 🧩 دریافت API Key از Hugging Face Secrets
|
| 6 |
OLLAMA_API_KEY = os.environ.get("OLLAMA_API_KEY")
|
| 7 |
-
|
| 8 |
if not OLLAMA_API_KEY:
|
| 9 |
-
raise ValueError("API Key تنظیم نشده است.
|
| 10 |
|
| 11 |
-
# 🧠 اتصال به Ollama Cloud
|
| 12 |
client = Client(
|
| 13 |
host="https://ollama.com",
|
| 14 |
headers={"Authorization": f"Bearer {OLLAMA_API_KEY}"}
|
| 15 |
)
|
| 16 |
|
| 17 |
-
# مدلهای Cloud در دسترس
|
| 18 |
AVAILABLE_MODELS = [
|
| 19 |
"gpt-oss:20b-cloud",
|
| 20 |
-
"
|
| 21 |
-
"qwen3-coder:480b-cloud",
|
| 22 |
-
"deepseek-v3.1:671b-cloud",
|
| 23 |
-
"kimi-k2:1t-cloud"
|
| 24 |
]
|
| 25 |
|
| 26 |
-
# تابع
|
| 27 |
-
def
|
| 28 |
messages = []
|
| 29 |
-
# تبدیل تاریخچه چت
|
| 30 |
for h in history:
|
| 31 |
messages.append({"role": "user", "content": h[0]})
|
| 32 |
messages.append({"role": "assistant", "content": h[1]})
|
| 33 |
messages.append({"role": "user", "content": message})
|
| 34 |
|
| 35 |
-
|
| 36 |
-
#
|
| 37 |
for part in client.chat(model_name, messages=messages, stream=True):
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
#
|
| 43 |
-
with gr.Blocks(
|
| 44 |
-
gr.Markdown("# 💬 Ollama Cloud Chatbot
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
)
|
| 52 |
-
|
| 53 |
-
chatbot = gr.ChatInterface(
|
| 54 |
-
fn=lambda msg, hist: chat_fn(msg, hist, model_selector.value),
|
| 55 |
-
title="Ollama Cloud Chatbot"
|
| 56 |
-
)
|
| 57 |
|
| 58 |
demo.launch()
|
|
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
from ollama import Client
|
| 4 |
+
import time
|
| 5 |
|
|
|
|
| 6 |
OLLAMA_API_KEY = os.environ.get("OLLAMA_API_KEY")
|
|
|
|
| 7 |
if not OLLAMA_API_KEY:
|
| 8 |
+
raise ValueError("API Key تنظیم نشده است.")
|
| 9 |
|
|
|
|
| 10 |
client = Client(
|
| 11 |
host="https://ollama.com",
|
| 12 |
headers={"Authorization": f"Bearer {OLLAMA_API_KEY}"}
|
| 13 |
)
|
| 14 |
|
|
|
|
| 15 |
AVAILABLE_MODELS = [
|
| 16 |
"gpt-oss:20b-cloud",
|
| 17 |
+
"qwen3-coder:480b-cloud"
|
|
|
|
|
|
|
|
|
|
| 18 |
]
|
| 19 |
|
| 20 |
+
# تابع استریمینگ
|
| 21 |
+
def respond(message, history, model_name):
|
| 22 |
messages = []
|
|
|
|
| 23 |
for h in history:
|
| 24 |
messages.append({"role": "user", "content": h[0]})
|
| 25 |
messages.append({"role": "assistant", "content": h[1]})
|
| 26 |
messages.append({"role": "user", "content": message})
|
| 27 |
|
| 28 |
+
bot_message = ""
|
| 29 |
+
yield history, "" # برای ریست کردن Textbox کاربر
|
| 30 |
for part in client.chat(model_name, messages=messages, stream=True):
|
| 31 |
+
bot_message += part["message"]["content"]
|
| 32 |
+
yield history + [(message, bot_message)], "" # بروزرسانی استریم
|
| 33 |
+
time.sleep(0.01) # کمی تاخیر برای ظاهر شدن استریم روان
|
| 34 |
+
|
| 35 |
+
# رابط Blocks
|
| 36 |
+
with gr.Blocks() as demo:
|
| 37 |
+
gr.Markdown("# 💬 Ollama Cloud Chatbot (Streamed)")
|
| 38 |
+
model_selector = gr.Dropdown(choices=AVAILABLE_MODELS, value=AVAILABLE_MODELS[0], label="انتخاب مدل")
|
| 39 |
+
chatbot = gr.Chatbot()
|
| 40 |
+
msg = gr.Textbox(label="پیام خود را وارد کنید")
|
| 41 |
+
send_btn = gr.Button("ارسال")
|
| 42 |
+
|
| 43 |
+
send_btn.click(respond, inputs=[msg, chatbot, model_selector], outputs=[chatbot, msg])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
demo.launch()
|