aigos commited on
Commit
4ab13d7
·
verified ·
1 Parent(s): 5000921

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -13
app.py CHANGED
@@ -2,8 +2,7 @@ import gradio as gr
2
  from huggingface_hub import InferenceClient
3
  import os
4
 
5
- # We gebruiken Llama-3.1-8B omdat deze het meest stabiel is op HF gratis servers.
6
- # Zorg dat je 'Request Access' hebt gedaan op de HF pagina van dit model!
7
  client = InferenceClient(
8
  "meta-llama/Llama-3.1-8B-Instruct",
9
  token=os.getenv("HF_TOKEN")
@@ -11,27 +10,27 @@ client = InferenceClient(
11
 
12
  def respond(
13
  message,
14
- history, # History is nu een lijst van dicts: [{"role": "user", "content": "..."}, ...]
15
  system_message,
16
  max_tokens,
17
  temperature,
18
  top_p,
19
  ):
20
- # We beginnen met de system prompt
21
  messages = [{"role": "system", "content": system_message}]
22
 
23
- # We voegen de geschiedenis toe aan de berichtenlijst
24
- # Gradio 5+ stuurt de geschiedenis al in het juiste format (dicts)
25
- for entry in history:
26
- messages.append(entry)
 
 
 
27
 
28
- # Voeg de nieuwste vraag van de gebruiker toe
29
  messages.append({"role": "user", "content": message})
30
 
31
  response = ""
32
 
33
  try:
34
- # Roep de AI aan
35
  for msg in client.chat_completion(
36
  messages,
37
  max_tokens=max_tokens,
@@ -43,12 +42,11 @@ def respond(
43
  response += token
44
  yield response
45
  except Exception as e:
46
- yield f"Verbindingsfout: {str(e)}. Probeer het over een momentje opnieuw."
47
 
48
- # De interface
49
  demo = gr.ChatInterface(
50
  respond,
51
- type="messages", # DIT IS DE BELANGRIJKE FIX: Vertelt Gradio het nieuwe format te gebruiken
52
  additional_inputs=[
53
  gr.Textbox(
54
  value="Bismillah. You are Noor AI, a wise Islamic scholar. Answer accurately based on Quran and Sunnah. End with Wa Allahu a'la wa a'lam.",
 
2
  from huggingface_hub import InferenceClient
3
  import os
4
 
5
+ # Gebruik Llama-3.1-8B-Instruct (Zorg dat je 'Request Access' hebt gedaan op HF!)
 
6
  client = InferenceClient(
7
  "meta-llama/Llama-3.1-8B-Instruct",
8
  token=os.getenv("HF_TOKEN")
 
10
 
11
  def respond(
12
  message,
13
+ history,
14
  system_message,
15
  max_tokens,
16
  temperature,
17
  top_p,
18
  ):
 
19
  messages = [{"role": "system", "content": system_message}]
20
 
21
+ # Veilige manier om geschiedenis te verwerken (werkt in Gradio 4, 5 en 6)
22
+ for val in history:
23
+ if isinstance(val, dict): # Moderne Gradio stijl
24
+ messages.append(val)
25
+ elif isinstance(val, (list, tuple)): # Klassieke Gradio stijl
26
+ if val[0]: messages.append({"role": "user", "content": val[0]})
27
+ if val[1]: messages.append({"role": "assistant", "content": val[1]})
28
 
 
29
  messages.append({"role": "user", "content": message})
30
 
31
  response = ""
32
 
33
  try:
 
34
  for msg in client.chat_completion(
35
  messages,
36
  max_tokens=max_tokens,
 
42
  response += token
43
  yield response
44
  except Exception as e:
45
+ yield f"Verbindingsfout: {str(e)}. Controleer of je 'Request Access' hebt gedaan voor Llama-3.1 op Hugging Face."
46
 
47
+ # GEEN 'type' argument meer, zodat het nooit meer crasht op TypeError
48
  demo = gr.ChatInterface(
49
  respond,
 
50
  additional_inputs=[
51
  gr.Textbox(
52
  value="Bismillah. You are Noor AI, a wise Islamic scholar. Answer accurately based on Quran and Sunnah. End with Wa Allahu a'la wa a'lam.",