Update app.py
Browse files
app.py
CHANGED
|
@@ -1,35 +1,45 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
import os
|
| 3 |
-
|
|
|
|
| 4 |
|
| 5 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 6 |
|
| 7 |
-
client =
|
| 8 |
-
|
| 9 |
api_key=HF_TOKEN,
|
| 10 |
)
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
temperature=0.7,
|
|
|
|
| 18 |
)
|
| 19 |
-
return
|
| 20 |
|
| 21 |
def chat_fn(message, history):
|
| 22 |
if history is None:
|
| 23 |
history = []
|
| 24 |
-
|
| 25 |
history.append({"role": "user", "content": message})
|
| 26 |
-
reply = query_mistral(message)
|
| 27 |
-
history.append({"role": "assistant", "content": reply})
|
| 28 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
return history
|
| 30 |
|
| 31 |
-
with gr.Blocks() as demo:
|
| 32 |
-
gr.Markdown("##
|
| 33 |
chatbot = gr.Chatbot(value=[])
|
| 34 |
msg = gr.Textbox(placeholder="Sor...")
|
| 35 |
msg.submit(chat_fn, [msg, chatbot], chatbot)
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from openai import OpenAI
|
| 4 |
|
| 5 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 6 |
|
| 7 |
+
client = OpenAI(
|
| 8 |
+
base_url="https://router.huggingface.co/v1",
|
| 9 |
api_key=HF_TOKEN,
|
| 10 |
)
|
| 11 |
|
| 12 |
+
MODEL_ID = "HuggingFaceTB/SmolLM3-3B:hf-inference"
|
| 13 |
+
|
| 14 |
+
SYSTEM = "Sen MAIND AI'sin. Kısa, net ve yardımcı cevap ver."
|
| 15 |
+
|
| 16 |
+
def llm_reply(user_text: str) -> str:
|
| 17 |
+
resp = client.chat.completions.create(
|
| 18 |
+
model=MODEL_ID,
|
| 19 |
+
messages=[
|
| 20 |
+
{"role": "system", "content": SYSTEM},
|
| 21 |
+
{"role": "user", "content": user_text},
|
| 22 |
+
],
|
| 23 |
temperature=0.7,
|
| 24 |
+
max_tokens=350,
|
| 25 |
)
|
| 26 |
+
return resp.choices[0].message.content or ""
|
| 27 |
|
| 28 |
def chat_fn(message, history):
|
| 29 |
if history is None:
|
| 30 |
history = []
|
|
|
|
| 31 |
history.append({"role": "user", "content": message})
|
|
|
|
|
|
|
| 32 |
|
| 33 |
+
try:
|
| 34 |
+
reply = llm_reply(message)
|
| 35 |
+
except Exception as e:
|
| 36 |
+
reply = f"Hata: {e}"
|
| 37 |
+
|
| 38 |
+
history.append({"role": "assistant", "content": reply})
|
| 39 |
return history
|
| 40 |
|
| 41 |
+
with gr.Blocks(title="MaindAI") as demo:
|
| 42 |
+
gr.Markdown("## MaindAI 🤖")
|
| 43 |
chatbot = gr.Chatbot(value=[])
|
| 44 |
msg = gr.Textbox(placeholder="Sor...")
|
| 45 |
msg.submit(chat_fn, [msg, chatbot], chatbot)
|