dimasdeffieux commited on
Commit
685f947
Β·
verified Β·
1 Parent(s): 9c864d2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -31
app.py CHANGED
@@ -1,69 +1,70 @@
1
  import gradio as gr
2
- from llama_cpp import Llama
3
  from paddleocr import PaddleOCR
4
  from PIL import Image
5
 
6
- # Load GGUF model
7
- llm = Llama(
8
- model_path="./deepseek-v3-0324.Q4_K_M.gguf", # Make sure this file is in your repo
9
- n_ctx=2048,
10
- n_threads=8,
11
- n_gpu_layers=20 # Set to 0 if you are on CPU-only
12
- )
13
-
14
- # OCR Function
15
- def ocr_inference(img, lang):
16
- ocr = PaddleOCR(use_angle_cls=True, lang=lang, use_gpu=False)
17
- result = ocr.ocr(img, cls=True)[0]
18
- txts = [line[1][0] for line in result]
19
- return " ".join(txts)
20
 
21
- # Step 1: Convert text to base form words
22
  def text_inference(text, language):
23
  prompt = (
24
  f"Given the following {language} text, convert each word into its base form. "
25
  f"Remove all duplicates. Return the base form words as a comma-separated list.\n\n"
26
  f"Text:\n{text}"
27
  )
28
- response = llm(prompt, max_tokens=256, stop=["</s>"])
29
- output_text = response["choices"][0]["text"].strip()
30
- words = [w.strip() for w in output_text.split(",") if w.strip()]
31
  return words
32
 
33
- # Step 2: Generate flashcards for those words
34
  def make_flashcards(words, language):
35
  prompt = (
36
  f"For each {language} word in the list, write a flashcard in this format:\n"
37
- f"word - definition - example sentence - translated sentence.\n\n"
38
  f"Words:\n{', '.join(words)}"
39
  )
40
- response = llm(prompt, max_tokens=512, stop=["</s>"])
41
- return response["choices"][0]["text"].strip()
 
 
 
 
 
 
42
 
43
- # Wrapper logic to handle OCR or text
44
  def flashcard_pipeline(text, image, language):
 
 
 
 
 
 
 
45
  if image:
46
- text = ocr_inference(image, language)
47
  if not text:
48
  return "", "Please provide either text or an image."
 
49
  words = text_inference(text, language)
50
  flashcards = make_flashcards(words, language)
51
  return "\n".join(words), flashcards
52
 
53
- # Gradio UI
54
  demo = gr.Interface(
55
  fn=flashcard_pipeline,
56
  inputs=[
57
- gr.Textbox(label="Input Text (leave empty to use image)", lines=4, placeholder="Type or paste sentence here..."),
58
- gr.Image(label="Upload Image for OCR (optional)", type="filepath"),
59
- gr.Dropdown(choices=["korean", "japan", "french", "ch"], label="Language (for OCR and LLM)")
60
  ],
61
  outputs=[
62
  gr.Textbox(label="Base Form Words"),
63
  gr.Textbox(label="Flashcards"),
64
  ],
65
- title="Language Flashcard Generator (with OCR + DeepSeek GGUF)",
66
- description="Either input text or upload an image. The app will extract words, lemmatize them, and generate flashcards."
67
  )
68
 
69
  if __name__ == "__main__":
 
1
  import gradio as gr
2
+ from huggingface_hub import InferenceClient
3
  from paddleocr import PaddleOCR
4
  from PIL import Image
5
 
6
+ # Use the hosted model
7
+ client = InferenceClient("unsloth/DeepSeek-V3-0324-GGUF")
 
 
 
 
 
 
 
 
 
 
 
 
8
 
9
+ # Extract words in base form
10
  def text_inference(text, language):
11
  prompt = (
12
  f"Given the following {language} text, convert each word into its base form. "
13
  f"Remove all duplicates. Return the base form words as a comma-separated list.\n\n"
14
  f"Text:\n{text}"
15
  )
16
+ response = client.text_generation(prompt, max_new_tokens=256, temperature=0.7)
17
+ words = [w.strip() for w in response.strip().split(",") if w.strip()]
 
18
  return words
19
 
20
+ # Create flashcards
21
  def make_flashcards(words, language):
22
  prompt = (
23
  f"For each {language} word in the list, write a flashcard in this format:\n"
24
+ f"Word: <word>\nDefinition: <definition>\nExample: <sentence>\nTranslation: <translation>\n\n"
25
  f"Words:\n{', '.join(words)}"
26
  )
27
+ response = client.text_generation(prompt, max_new_tokens=512, temperature=0.7)
28
+ return response.strip()
29
+
30
+ # OCR from image
31
+ def ocr_inference(img_path, lang_code):
32
+ ocr = PaddleOCR(use_angle_cls=True, lang=lang_code, use_gpu=False)
33
+ result = ocr.ocr(img_path, cls=True)[0]
34
+ return " ".join([line[1][0] for line in result])
35
 
36
+ # Combined pipeline
37
  def flashcard_pipeline(text, image, language):
38
+ lang_code = {
39
+ "korean": "korean",
40
+ "japanese": "japan",
41
+ "chinese": "ch",
42
+ "english": "en",
43
+ }.get(language.lower(), "en")
44
+
45
  if image:
46
+ text = ocr_inference(image, lang_code)
47
  if not text:
48
  return "", "Please provide either text or an image."
49
+
50
  words = text_inference(text, language)
51
  flashcards = make_flashcards(words, language)
52
  return "\n".join(words), flashcards
53
 
54
+ # Gradio app
55
  demo = gr.Interface(
56
  fn=flashcard_pipeline,
57
  inputs=[
58
+ gr.Textbox(label="Input Text (leave blank if using image)", lines=4, placeholder="e.g. ν‘œν˜„μ΄ μ„œνˆ° 것도 잘λͺ»μΈκ°€μš”..."),
59
+ gr.Image(type="filepath", label="Upload Image (optional, for OCR)"),
60
+ gr.Dropdown(["korean", "japanese", "chinese", "english"], label="Language"),
61
  ],
62
  outputs=[
63
  gr.Textbox(label="Base Form Words"),
64
  gr.Textbox(label="Flashcards"),
65
  ],
66
+ title="πŸ“˜ Language Flashcard Generator (OCR + LLM)",
67
+ description="Input text or image. It extracts words, finds base forms, and generates flashcards using DeepSeek-V3-0324.",
68
  )
69
 
70
  if __name__ == "__main__":