CCockrum commited on
Commit
63ecd90
·
verified ·
1 Parent(s): 831acca

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -42
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);
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;
30
  }
31
  #title-container h1 {
32
  color: #222 !important;
@@ -65,63 +65,55 @@ body {
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
- # FIXED: Cleanly add history entries
92
- if history:
93
- for h in history:
94
- if isinstance(h, dict):
95
- messages.append(h)
 
 
 
 
96
 
97
  messages.append({"role": "user", "content": message})
98
-
99
  response = ""
100
-
101
  try:
102
- stream = client.chat.completions.create(
103
- model="HuggingFaceH4/zephyr-7b-beta",
104
- messages=messages,
105
- stream=True,
106
  max_tokens=max_tokens,
 
107
  temperature=temperature,
108
  top_p=top_p,
109
- )
110
-
111
- for chunk in stream:
112
- # Handle the response object attributes correctly
113
- if hasattr(chunk, 'choices') and len(chunk.choices) > 0:
114
- delta = chunk.choices[0].delta
115
- if hasattr(delta, 'content') and delta.content:
116
- response += delta.content
117
- yield response
118
-
119
  except Exception as e:
120
  yield f"Désolé! There was an error: {str(e)}"
121
 
122
-
123
-
124
- # === GRADIO INTERFACE ===
125
  with gr.Blocks(css=css) as demo:
126
  gr.HTML("""
127
  <div id="title-container">
@@ -129,9 +121,8 @@ with gr.Blocks(css=css) as demo:
129
  <p class="subtitle">French Tutor</p>
130
  </div>
131
  """)
132
-
133
  with gr.Column(elem_id="chat-panel"):
134
-
135
  with gr.Accordion("Advanced Settings", open=False):
136
  user_level = gr.Dropdown(
137
  choices=["A1", "A2", "B1", "B2", "C1", "C2"],
@@ -149,4 +140,4 @@ with gr.Blocks(css=css) as demo:
149
  )
150
 
151
  if __name__ == "__main__":
152
- demo.launch()
 
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
  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
  }
66
  """
67
 
68
+ # Model client (consider switching to a public model like mistralai if 401 persists)
69
+ client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
 
 
 
70
 
71
  # Level prompt selector
72
  def level_to_prompt(level):
73
  return {
74
+ "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.",
75
+ "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.",
76
+ "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.",
77
+ "B2": "You are a French tutor. Respond primarily in French and include only concise, relevant elaborations. Avoid suggesting voice interaction or unrelated content.",
78
+ "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.",
79
+ "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."
80
  }.get(level, "You are a helpful French tutor.")
81
 
82
 
83
+ # Chat handler
84
  def respond(message, history, user_level, max_tokens, temperature, top_p):
85
  system_message = level_to_prompt(user_level)
86
  messages = [{"role": "system", "content": system_message}]
87
+
88
+ # Handle history
89
+ if history and isinstance(history[0], tuple):
90
+ for user_msg, assistant_msg in history:
91
+ if user_msg:
92
+ messages.append({"role": "user", "content": user_msg})
93
+ if assistant_msg:
94
+ messages.append({"role": "assistant", "content": assistant_msg})
95
+ else:
96
+ messages.extend(history)
97
 
98
  messages.append({"role": "user", "content": message})
99
+
100
  response = ""
 
101
  try:
102
+ for msg in client.chat_completion(
103
+ messages,
 
 
104
  max_tokens=max_tokens,
105
+ stream=True,
106
  temperature=temperature,
107
  top_p=top_p,
108
+ ):
109
+ token = msg.choices[0].delta.content
110
+ if token:
111
+ response += token
112
+ yield response
 
 
 
 
 
113
  except Exception as e:
114
  yield f"Désolé! There was an error: {str(e)}"
115
 
116
+ # Gradio interface
 
 
117
  with gr.Blocks(css=css) as demo:
118
  gr.HTML("""
119
  <div id="title-container">
 
121
  <p class="subtitle">French Tutor</p>
122
  </div>
123
  """)
124
+
125
  with gr.Column(elem_id="chat-panel"):
 
126
  with gr.Accordion("Advanced Settings", open=False):
127
  user_level = gr.Dropdown(
128
  choices=["A1", "A2", "B1", "B2", "C1", "C2"],
 
140
  )
141
 
142
  if __name__ == "__main__":
143
+ demo.launch()