Teotonix commited on
Commit
aa05679
·
verified ·
1 Parent(s): 139e7e2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -15
app.py CHANGED
@@ -1,35 +1,45 @@
1
- import gradio as gr
2
  import os
3
- from huggingface_hub import InferenceClient
 
4
 
5
  HF_TOKEN = os.getenv("HF_TOKEN")
6
 
7
- client = InferenceClient(
8
- provider="hf-inference",
9
  api_key=HF_TOKEN,
10
  )
11
 
12
- def query_mistral(prompt):
13
- response = client.text_generation(
14
- model="mistralai/Mistral-7B-Instruct-v0.2",
15
- prompt=prompt,
16
- max_new_tokens=300,
 
 
 
 
 
 
17
  temperature=0.7,
 
18
  )
19
- return response
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("## Maind.ai 🤖 (Mistral)")
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)