Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -2,13 +2,30 @@ import gradio as gr
|
|
2 |
from huggingface_hub import InferenceClient
|
3 |
import os
|
4 |
|
|
|
5 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
6 |
-
if HF_TOKEN:
|
7 |
-
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta", token=HF_TOKEN)
|
8 |
-
else:
|
9 |
-
client = InferenceClient("mistralai/Mistral-7B-Instruct-v0.1")
|
10 |
|
11 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
def level_to_prompt(level):
|
13 |
return {
|
14 |
"A1": "You are a friendly French tutor. Speak mostly in English, use simple French, and explain everything.",
|
@@ -19,7 +36,7 @@ def level_to_prompt(level):
|
|
19 |
"C2": "You are a native French professor. Speak in rich, complex French. Avoid English."
|
20 |
}.get(level, "You are a helpful French tutor.")
|
21 |
|
22 |
-
#Custom background CSS
|
23 |
css = """
|
24 |
@import url('https://fonts.googleapis.com/css2?family=Noto+Sans+JP&family=Playfair+Display&display=swap');
|
25 |
body {
|
@@ -57,9 +74,13 @@ body {
|
|
57 |
}
|
58 |
"""
|
59 |
|
60 |
-
|
61 |
-
#Chat logic
|
62 |
def respond(message, history, level, max_tokens, temperature, top_p):
|
|
|
|
|
|
|
|
|
|
|
63 |
system_message = level_to_prompt(level)
|
64 |
messages = [{"role": "system", "content": system_message}]
|
65 |
|
@@ -81,36 +102,98 @@ def respond(message, history, level, max_tokens, temperature, top_p):
|
|
81 |
# Generate response
|
82 |
response = ""
|
83 |
try:
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
90 |
response += token
|
91 |
-
|
|
|
92 |
except Exception as e:
|
|
|
93 |
print(f"Error in chat completion: {e}")
|
94 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
95 |
|
96 |
-
#UI layout
|
97 |
-
with gr.Blocks(css=css) as demo:
|
98 |
-
gr.Markdown("French Tutor", elem_id="custom-title")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
99 |
with gr.Column(elem_id="chat-panel"):
|
100 |
with gr.Accordion("⚙️ Advanced Settings", open=False):
|
101 |
level = gr.Dropdown(
|
102 |
choices=["A1", "A2", "B1", "B2", "C1", "C2"],
|
103 |
value="A1",
|
104 |
-
label="Your French Level (CEFR)"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
105 |
)
|
106 |
-
max_tokens = gr.Slider(1, 2048, value=512, step=1, label="Response Length")
|
107 |
-
temperature = gr.Slider(0.1, 4.0, value=0.7, step=0.1, label="Creativity")
|
108 |
-
top_p = gr.Slider(0.1, 1.0, value=0.95, step=0.05, label="Dynamic Text")
|
109 |
|
110 |
gr.ChatInterface(
|
111 |
fn=respond,
|
112 |
additional_inputs=[level, max_tokens, temperature, top_p],
|
113 |
-
type="messages"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
114 |
)
|
115 |
|
116 |
if __name__ == "__main__":
|
|
|
2 |
from huggingface_hub import InferenceClient
|
3 |
import os
|
4 |
|
5 |
+
# Get HF token from environment variable
|
6 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
|
|
|
|
|
|
|
|
7 |
|
8 |
+
# Initialize client with proper error handling
|
9 |
+
def get_client():
|
10 |
+
if HF_TOKEN:
|
11 |
+
try:
|
12 |
+
# Try with the preferred model first
|
13 |
+
return InferenceClient("HuggingFaceH4/zephyr-7b-beta", token=HF_TOKEN)
|
14 |
+
except Exception as e:
|
15 |
+
print(f"Failed to initialize zephyr model: {e}")
|
16 |
+
# Fallback to mistral with token
|
17 |
+
try:
|
18 |
+
return InferenceClient("mistralai/Mistral-7B-Instruct-v0.1", token=HF_TOKEN)
|
19 |
+
except Exception as e2:
|
20 |
+
print(f"Failed to initialize mistral model: {e2}")
|
21 |
+
return None
|
22 |
+
else:
|
23 |
+
print("No HF_TOKEN found. Please set your Hugging Face token.")
|
24 |
+
return None
|
25 |
+
|
26 |
+
client = get_client()
|
27 |
+
|
28 |
+
# Dynamic prompt builder based on CEFR level
|
29 |
def level_to_prompt(level):
|
30 |
return {
|
31 |
"A1": "You are a friendly French tutor. Speak mostly in English, use simple French, and explain everything.",
|
|
|
36 |
"C2": "You are a native French professor. Speak in rich, complex French. Avoid English."
|
37 |
}.get(level, "You are a helpful French tutor.")
|
38 |
|
39 |
+
# Custom background CSS
|
40 |
css = """
|
41 |
@import url('https://fonts.googleapis.com/css2?family=Noto+Sans+JP&family=Playfair+Display&display=swap');
|
42 |
body {
|
|
|
74 |
}
|
75 |
"""
|
76 |
|
77 |
+
# Chat logic with proper error handling
|
|
|
78 |
def respond(message, history, level, max_tokens, temperature, top_p):
|
79 |
+
# Check if client is available
|
80 |
+
if client is None:
|
81 |
+
yield "❌ Désolé! The AI service is not available. Please check your Hugging Face token configuration."
|
82 |
+
return
|
83 |
+
|
84 |
system_message = level_to_prompt(level)
|
85 |
messages = [{"role": "system", "content": system_message}]
|
86 |
|
|
|
102 |
# Generate response
|
103 |
response = ""
|
104 |
try:
|
105 |
+
# Create a proper prompt format
|
106 |
+
prompt = ""
|
107 |
+
for msg in messages:
|
108 |
+
if msg["role"] == "system":
|
109 |
+
prompt += f"System: {msg['content']}\n"
|
110 |
+
elif msg["role"] == "user":
|
111 |
+
prompt += f"User: {msg['content']}\n"
|
112 |
+
elif msg["role"] == "assistant":
|
113 |
+
prompt += f"Assistant: {msg['content']}\n"
|
114 |
+
prompt += "Assistant: "
|
115 |
+
|
116 |
+
# Generate response with streaming
|
117 |
+
for token in client.text_generation(
|
118 |
+
prompt,
|
119 |
+
max_new_tokens=max_tokens,
|
120 |
+
stream=True,
|
121 |
+
temperature=temperature,
|
122 |
+
top_p=top_p,
|
123 |
+
do_sample=True,
|
124 |
+
return_full_text=False
|
125 |
+
):
|
126 |
+
if token: # Handle None tokens
|
127 |
response += token
|
128 |
+
yield response
|
129 |
+
|
130 |
except Exception as e:
|
131 |
+
error_msg = str(e)
|
132 |
print(f"Error in chat completion: {e}")
|
133 |
+
|
134 |
+
if "401" in error_msg or "Unauthorized" in error_msg:
|
135 |
+
yield "🔑 Authentication Error: Please check your Hugging Face token. Make sure it's valid and has the correct permissions."
|
136 |
+
elif "429" in error_msg or "rate limit" in error_msg.lower():
|
137 |
+
yield "⏰ Rate limit exceeded. Please wait a moment before trying again."
|
138 |
+
elif "503" in error_msg or "Service Unavailable" in error_msg:
|
139 |
+
yield "🔧 The AI service is temporarily unavailable. Please try again later."
|
140 |
+
else:
|
141 |
+
yield f"❌ Désolé! There was an error: {error_msg}"
|
142 |
|
143 |
+
# UI layout
|
144 |
+
with gr.Blocks(css=css, title="French Tutor") as demo:
|
145 |
+
gr.Markdown("# 🇫🇷 French Tutor", elem_id="custom-title")
|
146 |
+
|
147 |
+
# Add status indicator
|
148 |
+
if client is None:
|
149 |
+
gr.Markdown("⚠️ **Warning**: No Hugging Face token found. Please set your HF_TOKEN environment variable.")
|
150 |
+
else:
|
151 |
+
gr.Markdown("✅ **Status**: Connected to AI service")
|
152 |
+
|
153 |
with gr.Column(elem_id="chat-panel"):
|
154 |
with gr.Accordion("⚙️ Advanced Settings", open=False):
|
155 |
level = gr.Dropdown(
|
156 |
choices=["A1", "A2", "B1", "B2", "C1", "C2"],
|
157 |
value="A1",
|
158 |
+
label="Your French Level (CEFR)",
|
159 |
+
info="Choose your current French proficiency level"
|
160 |
+
)
|
161 |
+
max_tokens = gr.Slider(
|
162 |
+
1, 2048,
|
163 |
+
value=512,
|
164 |
+
step=1,
|
165 |
+
label="Response Length",
|
166 |
+
info="Maximum number of tokens in the response"
|
167 |
+
)
|
168 |
+
temperature = gr.Slider(
|
169 |
+
0.1, 2.0,
|
170 |
+
value=0.7,
|
171 |
+
step=0.1,
|
172 |
+
label="Creativity",
|
173 |
+
info="Higher values make responses more creative"
|
174 |
+
)
|
175 |
+
top_p = gr.Slider(
|
176 |
+
0.1, 1.0,
|
177 |
+
value=0.95,
|
178 |
+
step=0.05,
|
179 |
+
label="Dynamic Text",
|
180 |
+
info="Controls text diversity"
|
181 |
)
|
|
|
|
|
|
|
182 |
|
183 |
gr.ChatInterface(
|
184 |
fn=respond,
|
185 |
additional_inputs=[level, max_tokens, temperature, top_p],
|
186 |
+
type="messages",
|
187 |
+
title="Chat with your French Tutor",
|
188 |
+
description="Ask questions, practice conversation, or get help with French grammar!",
|
189 |
+
examples=[
|
190 |
+
"Bonjour! Comment allez-vous?",
|
191 |
+
"Can you help me conjugate the verb 'être'?",
|
192 |
+
"What's the difference between 'tu' and 'vous'?",
|
193 |
+
"Tell me about French culture",
|
194 |
+
"How do I order food in a French restaurant?"
|
195 |
+
],
|
196 |
+
cache_examples=False
|
197 |
)
|
198 |
|
199 |
if __name__ == "__main__":
|