2ba commited on
Commit
31c6a1a
·
verified ·
1 Parent(s): 08658e2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -28
app.py CHANGED
@@ -28,13 +28,10 @@ def load_llm():
28
  SYSTEM_PROMPT = "به فارسی، روشن و کوتاه پاسخ بده (حداکثر ۲ جمله)."
29
 
30
  def respond(message, history):
31
- # 1) message می‌تونه dict یا str باشد
32
- if isinstance(message, dict):
33
- user_text = message.get("content", "")
34
- else:
35
- user_text = str(message or "")
36
 
37
- # 2) history می‌تونه tuples یا messages باشد
38
  msgs = [{"role": "system", "content": SYSTEM_PROMPT}]
39
  if history and isinstance(history[0], dict) and "role" in history[0]:
40
  msgs.extend(history)
@@ -45,29 +42,18 @@ def respond(message, history):
45
  msgs.append({"role": "user", "content": user_text})
46
 
47
  llm = load_llm()
48
- print(">> gen start")
 
 
 
 
 
 
 
 
 
 
49
 
50
- partial = ""
51
- try:
52
- for chunk in llm.create_chat_completion(
53
- messages=msgs,
54
- max_tokens=64,
55
- temperature=0.4,
56
- top_p=0.9,
57
- stream=True,
58
- ):
59
- choice = chunk["choices"][0]
60
- delta = choice.get("delta") or {}
61
- token = delta.get("content") or ""
62
- if token:
63
- partial += token
64
- yield partial
65
- if choice.get("finish_reason"):
66
- break
67
- print(">> gen done")
68
- except Exception as e:
69
- print(">> exception:", repr(e))
70
- yield "متاسفم، در تولید پاسخ خطایی رخ داد. دوباره امتحان کن."
71
 
72
  demo = gr.ChatInterface(
73
  fn=respond,
 
28
  SYSTEM_PROMPT = "به فارسی، روشن و کوتاه پاسخ بده (حداکثر ۲ جمله)."
29
 
30
  def respond(message, history):
31
+ # message می‌تواند dict یا str باشد
32
+ user_text = message.get("content", "") if isinstance(message, dict) else str(message or "")
 
 
 
33
 
34
+ # history می‌تواند tuples یا messages باشد
35
  msgs = [{"role": "system", "content": SYSTEM_PROMPT}]
36
  if history and isinstance(history[0], dict) and "role" in history[0]:
37
  msgs.extend(history)
 
42
  msgs.append({"role": "user", "content": user_text})
43
 
44
  llm = load_llm()
45
+ print(">> gen start (non-stream)")
46
+ out = llm.create_chat_completion(
47
+ messages=msgs,
48
+ max_tokens=64,
49
+ temperature=0.4,
50
+ top_p=0.9,
51
+ stream=False,
52
+ )
53
+ text = out["choices"][0]["message"]["content"]
54
+ print(">> gen done (non-stream)")
55
+ return text
56
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
57
 
58
  demo = gr.ChatInterface(
59
  fn=respond,