pauladeeptech commited on
Commit
18b1e1b
·
verified ·
1 Parent(s): 9f37beb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -19
app.py CHANGED
@@ -1,6 +1,13 @@
1
  import os
2
  import requests
3
  import gradio as gr
 
 
 
 
 
 
 
4
 
5
  SYSTEM_PROMPT = """Du bist EcoHelper, eine entspannte und motivierende Nachhaltigkeits-Coachin für Teenagerinnen (13-15 Jahre).
6
 
@@ -13,12 +20,10 @@ Regeln:
13
  - Keine Ratschläge zu: Gesundheit, Geld, Politik
14
  - Wenn du etwas nicht genau weißt, sage: "Das weiß ich nicht sicher, aber du könntest..."
15
  - Bleib beim Thema Nachhaltigkeit
 
16
  """
17
 
18
- # Pick a small, usually-available instruct/chat model
19
  MODEL_ID = "HuggingFaceTB/SmolLM3-3B"
20
-
21
-
22
  ROUTER_CHAT_URL = "https://router.huggingface.co/v1/chat/completions"
23
 
24
  def chat(user_message, history):
@@ -48,7 +53,13 @@ def chat(user_message, history):
48
  }
49
 
50
  try:
51
- with requests.post(ROUTER_CHAT_URL, headers=headers, json=payload, stream=True, timeout=90) as r:
 
 
 
 
 
 
52
  if r.status_code != 200:
53
  print("HF Router error:", r.status_code, r.text)
54
  yield "Upsi — gerade ist das Modell nicht erreichbar. 🌍 Probier es bitte gleich nochmal!"
@@ -59,20 +70,26 @@ def chat(user_message, history):
59
  for line in r.iter_lines(decode_unicode=True):
60
  if not line:
61
  continue
62
- if line.startswith("data: "):
63
- data = line[len("data: "):].strip()
64
- if data == "[DONE]":
65
- break
66
- # Each data chunk is JSON
67
- try:
68
- chunk = __import__("json").loads(data)
69
- delta = chunk["choices"][0].get("delta", {}).get("content")
70
- if delta:
71
- response += delta
72
- yield response
73
- except Exception:
74
- # Ignore malformed chunks
75
- continue
 
 
 
 
 
 
76
 
77
  except Exception as e:
78
  print("Request failed:", repr(e))
@@ -87,9 +104,9 @@ demo = gr.ChatInterface(
87
  "Wie kann ich weniger Müll produzieren?",
88
  "Was ist Upcycling?",
89
  "Wie kann ich Wasser sparen?",
 
90
  ],
91
  )
92
 
93
  if __name__ == "__main__":
94
  demo.launch()
95
-
 
1
  import os
2
  import requests
3
  import gradio as gr
4
+ import re
5
+ import json
6
+
7
+ def clean_response(text: str) -> str:
8
+ # Remove <think>...</think> blocks completely (including multiline)
9
+ text = re.sub(r"<think>.*?</think>", "", text, flags=re.DOTALL | re.IGNORECASE)
10
+ return text.strip()
11
 
12
  SYSTEM_PROMPT = """Du bist EcoHelper, eine entspannte und motivierende Nachhaltigkeits-Coachin für Teenagerinnen (13-15 Jahre).
13
 
 
20
  - Keine Ratschläge zu: Gesundheit, Geld, Politik
21
  - Wenn du etwas nicht genau weißt, sage: "Das weiß ich nicht sicher, aber du könntest..."
22
  - Bleib beim Thema Nachhaltigkeit
23
+ - Zeige niemals interne Gedanken oder <think>-Text.
24
  """
25
 
 
26
  MODEL_ID = "HuggingFaceTB/SmolLM3-3B"
 
 
27
  ROUTER_CHAT_URL = "https://router.huggingface.co/v1/chat/completions"
28
 
29
  def chat(user_message, history):
 
53
  }
54
 
55
  try:
56
+ with requests.post(
57
+ ROUTER_CHAT_URL,
58
+ headers=headers,
59
+ json=payload,
60
+ stream=True,
61
+ timeout=90
62
+ ) as r:
63
  if r.status_code != 200:
64
  print("HF Router error:", r.status_code, r.text)
65
  yield "Upsi — gerade ist das Modell nicht erreichbar. 🌍 Probier es bitte gleich nochmal!"
 
70
  for line in r.iter_lines(decode_unicode=True):
71
  if not line:
72
  continue
73
+ if not line.startswith("data: "):
74
+ continue
75
+
76
+ data = line[len("data: "):].strip()
77
+
78
+ if data == "[DONE]":
79
+ # Final cleanup on completion
80
+ yield clean_response(response)
81
+ break
82
+
83
+ try:
84
+ chunk = json.loads(data)
85
+ delta = chunk["choices"][0].get("delta", {}).get("content")
86
+ if delta:
87
+ response += delta
88
+ # Clean while streaming so <think> never appears
89
+ yield clean_response(response)
90
+ except Exception:
91
+ # Ignore malformed chunks
92
+ continue
93
 
94
  except Exception as e:
95
  print("Request failed:", repr(e))
 
104
  "Wie kann ich weniger Müll produzieren?",
105
  "Was ist Upcycling?",
106
  "Wie kann ich Wasser sparen?",
107
+ "Ist Fliegen schlecht für die Umwelt?",
108
  ],
109
  )
110
 
111
  if __name__ == "__main__":
112
  demo.launch()