Spaces:
Sleeping
Sleeping
Luigi
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -1,6 +1,6 @@
|
|
1 |
import os
|
2 |
import gradio as gr
|
3 |
-
from weasyprint import HTML #
|
4 |
from huggingface_hub import InferenceClient
|
5 |
from huggingface_hub.utils import HfHubHTTPError
|
6 |
import requests
|
@@ -14,7 +14,7 @@ MODEL_NAME = "meta-llama/Llama-3.1-8B-Instruct"
|
|
14 |
MAX_INPUT_LENGTH = 4096 # Maximum characters for lesson content
|
15 |
|
16 |
# --- Hugging Face Inference Client ---
|
17 |
-
client = InferenceClient(
|
18 |
|
19 |
def call_hf_api(lesson_text: str) -> str:
|
20 |
"""
|
@@ -179,27 +179,23 @@ Do NOT include any other text outside of the HTML. Output ONLY the HTML. The HT
|
|
179 |
]
|
180 |
|
181 |
try:
|
182 |
-
|
183 |
-
|
184 |
-
|
185 |
-
|
186 |
-
|
187 |
-
|
188 |
-
|
189 |
-
|
190 |
-
|
191 |
-
|
192 |
-
|
193 |
-
|
194 |
-
|
195 |
-
|
196 |
-
|
197 |
-
if "```" in generated_html:
|
198 |
-
generated_html = generated_html.split("```")[0]
|
199 |
|
200 |
|
201 |
-
return generated_html
|
202 |
-
|
203 |
except HfHubHTTPError as e:
|
204 |
return f"Error from Hugging Face Hub: {e}"
|
205 |
except requests.exceptions.RequestException as e:
|
@@ -208,6 +204,7 @@ Do NOT include any other text outside of the HTML. Output ONLY the HTML. The HT
|
|
208 |
return f"An unexpected error occurred: {e}"
|
209 |
|
210 |
|
|
|
211 |
def generate_exercises(lesson_text: str) -> str:
|
212 |
"""Generates exercises in HTML format."""
|
213 |
return call_hf_api(lesson_text)
|
@@ -245,7 +242,7 @@ with gr.Blocks(title="HSK Exercise Generator", theme=gr.themes.Soft()) as demo:
|
|
245 |
)
|
246 |
|
247 |
generate_btn = gr.Button("Generate Exercises")
|
248 |
-
output_html = gr.HTML(label="Generated Exercises") # Use gr.HTML
|
249 |
|
250 |
with gr.Row():
|
251 |
download_btn = gr.Button("Download as PDF")
|
|
|
1 |
import os
|
2 |
import gradio as gr
|
3 |
+
from weasyprint import HTML # Use WeasyPrint for HTML to PDF
|
4 |
from huggingface_hub import InferenceClient
|
5 |
from huggingface_hub.utils import HfHubHTTPError
|
6 |
import requests
|
|
|
14 |
MAX_INPUT_LENGTH = 4096 # Maximum characters for lesson content
|
15 |
|
16 |
# --- Hugging Face Inference Client ---
|
17 |
+
client = InferenceClient(model=MODEL_NAME, token=HF_TOKEN) # Corrected: Use model and token
|
18 |
|
19 |
def call_hf_api(lesson_text: str) -> str:
|
20 |
"""
|
|
|
179 |
]
|
180 |
|
181 |
try:
|
182 |
+
# Corrected: Use the text-generation task and pass messages directly
|
183 |
+
generated_text = client.text_generation(
|
184 |
+
prompt=prompt,
|
185 |
+
max_new_tokens=2000,
|
186 |
+
temperature=0.7,
|
187 |
+
top_p=0.9,
|
188 |
+
stop_sequences=["```"],
|
189 |
+
)
|
190 |
+
|
191 |
+
# HTML Cleanup:
|
192 |
+
if "```html" in generated_text:
|
193 |
+
generated_text = generated_text.split("```html")[1]
|
194 |
+
if "```" in generated_text:
|
195 |
+
generated_text = generated_text.split("```")[0]
|
196 |
+
return generated_text
|
|
|
|
|
197 |
|
198 |
|
|
|
|
|
199 |
except HfHubHTTPError as e:
|
200 |
return f"Error from Hugging Face Hub: {e}"
|
201 |
except requests.exceptions.RequestException as e:
|
|
|
204 |
return f"An unexpected error occurred: {e}"
|
205 |
|
206 |
|
207 |
+
|
208 |
def generate_exercises(lesson_text: str) -> str:
|
209 |
"""Generates exercises in HTML format."""
|
210 |
return call_hf_api(lesson_text)
|
|
|
242 |
)
|
243 |
|
244 |
generate_btn = gr.Button("Generate Exercises")
|
245 |
+
output_html = gr.HTML(label="Generated Exercises") # Use gr.HTML for HTML output
|
246 |
|
247 |
with gr.Row():
|
248 |
download_btn = gr.Button("Download as PDF")
|