Luigi commited on
Commit
a88cae5
·
verified ·
1 Parent(s): 8858676

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -22
app.py CHANGED
@@ -1,6 +1,6 @@
1
  import os
2
  import gradio as gr
3
- from weasyprint import HTML # For direct HTML to PDF conversion
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(api_key=HF_TOKEN)
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
- # Use the chat completions API correctly
183
- response = client.chat_completions.create(
184
- model=MODEL_NAME,
185
- messages=messages,
186
- max_tokens=2000, # Increased for HTML output
187
- temperature=0.7,
188
- top_p=0.9,
189
- stop=["```"], # Stop at the closing code fence
190
- )
191
- # Extract the generated content
192
- generated_html = response.choices[0].message.content
193
-
194
- # Sometimes LLMs add extra text before or after the HTML. This cleans it up.
195
- if "```html" in generated_html:
196
- generated_html = generated_html.split("```html")[1]
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")