CCockrum commited on
Commit
8d0a0a9
·
verified ·
1 Parent(s): 423fd42

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -21
app.py CHANGED
@@ -18,7 +18,7 @@ body {
18
  padding-bottom: 2rem;
19
  }
20
  #title-container {
21
- background-color: rgba(255, 255, 255, 0.85); /* match chat panel */
22
  border-radius: 16px;
23
  padding: 1.5rem 2rem;
24
  margin: 2rem 0;
@@ -26,7 +26,7 @@ body {
26
  max-width: 500px;
27
  text-align: left;
28
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
29
- margin-left: 0rem; /* Aligns left edge */
30
  }
31
  #title-container h1 {
32
  color: #222 !important;
@@ -65,29 +65,29 @@ body {
65
  }
66
  """
67
 
68
- # Model client (consider switching to a public model like mistralai if 401 persists)
 
69
  client = InferenceClient(
70
- base_url="https://router.huggingface.co/v1",
71
-
72
  )
73
 
74
  # Level prompt selector
75
  def level_to_prompt(level):
76
  return {
77
- "A1": "You are a friendly French tutor. Focus on the user's specific question. Use simple French and explain in English. If helpful, you may include word origins or cultural notes, but avoid unrelated tangents and voice features.",
78
- "A2": "You are a patient French tutor. Respond to the user's question clearly. You may include brief relevant background such as word origin, common mistakes, or cultural usage — but only if directly related to the question. Do not mention or suggest voice interaction.",
79
- "B1": "You are a helpful French tutor. Use mostly French and minimal English. You can add short on-topic insights (like grammar tips or usage context) but avoid unrelated vocabulary or tools.",
80
- "B2": "You are a French tutor. Respond primarily in French and include only concise, relevant elaborations. Avoid suggesting voice interaction or unrelated content.",
81
- "C1": "You are a native French tutor. Use fluent French and address only what was asked, but you may include brief cultural or historical context if directly relevant.",
82
- "C2": "You are a French language professor. Use sophisticated French to answer only the question. You may include historical or linguistic nuance but avoid speculation or tool suggestions."
83
  }.get(level, "You are a helpful French tutor.")
84
 
85
 
86
- # Chat handler
87
  def respond(message, history, user_level, max_tokens, temperature, top_p):
88
  system_message = level_to_prompt(user_level)
89
  messages = [{"role": "system", "content": system_message}]
90
-
91
  # Handle history
92
  if history and isinstance(history[0], tuple):
93
  for user_msg, assistant_msg in history:
@@ -99,30 +99,34 @@ def respond(message, history, user_level, max_tokens, temperature, top_p):
99
  messages.extend(history)
100
 
101
  messages.append({"role": "user", "content": message})
102
-
103
  response = ""
 
104
  try:
105
- for msg in client.chat.completions.create(
106
  model="HuggingFaceH4/zephyr-7b-beta",
107
  messages=messages,
108
  stream=True,
109
  max_tokens=max_tokens,
110
  temperature=temperature,
111
  top_p=top_p,
112
- ):
113
- # Streaming chunks from HF arrive as dicts
 
 
114
  delta = msg.get("choices", [{}])[0].get("delta", {})
115
  token = delta.get("content", "")
116
-
117
  if token:
118
  response += token
119
-
120
  yield response
121
 
122
  except Exception as e:
123
  yield f"Désolé! There was an error: {str(e)}"
124
 
125
- # Gradio interface
 
126
  with gr.Blocks(css=css) as demo:
127
  gr.HTML("""
128
  <div id="title-container">
@@ -130,8 +134,9 @@ with gr.Blocks(css=css) as demo:
130
  <p class="subtitle">French Tutor</p>
131
  </div>
132
  """)
133
-
134
  with gr.Column(elem_id="chat-panel"):
 
135
  with gr.Accordion("Advanced Settings", open=False):
136
  user_level = gr.Dropdown(
137
  choices=["A1", "A2", "B1", "B2", "C1", "C2"],
 
18
  padding-bottom: 2rem;
19
  }
20
  #title-container {
21
+ background-color: rgba(255, 255, 255, 0.85);
22
  border-radius: 16px;
23
  padding: 1.5rem 2rem;
24
  margin: 2rem 0;
 
26
  max-width: 500px;
27
  text-align: left;
28
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
29
+ margin-left: 0rem;
30
  }
31
  #title-container h1 {
32
  color: #222 !important;
 
65
  }
66
  """
67
 
68
+ # === PATCHED CLIENT ===
69
+ # Uses the new unified HF router for OpenAI-like chat completion
70
  client = InferenceClient(
71
+ base_url="https://router.huggingface.co/v1"
 
72
  )
73
 
74
  # Level prompt selector
75
  def level_to_prompt(level):
76
  return {
77
+ "A1": "You are a friendly French tutor. Focus on the user's specific question. Use simple French and explain in English.",
78
+ "A2": "You are a patient French tutor. Respond to the user's question clearly and simply.",
79
+ "B1": "You are a helpful French tutor. Use mostly French with minimal English when needed.",
80
+ "B2": "You are a French tutor. Respond primarily in French with concise, relevant explanations.",
81
+ "C1": "You are a native French tutor. Use fluent French and include brief cultural or historical notes when relevant.",
82
+ "C2": "You are a French language professor. Use sophisticated French with precise explanations.",
83
  }.get(level, "You are a helpful French tutor.")
84
 
85
 
86
+ # === PATCHED CHAT HANDLER ===
87
  def respond(message, history, user_level, max_tokens, temperature, top_p):
88
  system_message = level_to_prompt(user_level)
89
  messages = [{"role": "system", "content": system_message}]
90
+
91
  # Handle history
92
  if history and isinstance(history[0], tuple):
93
  for user_msg, assistant_msg in history:
 
99
  messages.extend(history)
100
 
101
  messages.append({"role": "user", "content": message})
102
+
103
  response = ""
104
+
105
  try:
106
+ stream = client.chat.completions.create(
107
  model="HuggingFaceH4/zephyr-7b-beta",
108
  messages=messages,
109
  stream=True,
110
  max_tokens=max_tokens,
111
  temperature=temperature,
112
  top_p=top_p,
113
+ )
114
+
115
+ for msg in stream:
116
+ # NEW: Streaming dict-safe extraction
117
  delta = msg.get("choices", [{}])[0].get("delta", {})
118
  token = delta.get("content", "")
119
+
120
  if token:
121
  response += token
122
+
123
  yield response
124
 
125
  except Exception as e:
126
  yield f"Désolé! There was an error: {str(e)}"
127
 
128
+
129
+ # === GRADIO INTERFACE ===
130
  with gr.Blocks(css=css) as demo:
131
  gr.HTML("""
132
  <div id="title-container">
 
134
  <p class="subtitle">French Tutor</p>
135
  </div>
136
  """)
137
+
138
  with gr.Column(elem_id="chat-panel"):
139
+
140
  with gr.Accordion("Advanced Settings", open=False):
141
  user_level = gr.Dropdown(
142
  choices=["A1", "A2", "B1", "B2", "C1", "C2"],