Nayera-2025 commited on
Commit
373f022
·
verified ·
1 Parent(s): 04b3f18

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +7 -7
app.py CHANGED
@@ -1,23 +1,23 @@
1
  import gradio as gr
2
  from paddleocr import PaddleOCR
3
  import numpy as np
4
- import openai
5
  import os
6
  from langdetect import detect
 
 
7
 
8
  # Initialize PaddleOCR
9
  ocr_reader = PaddleOCR(use_angle_cls=True, lang='en')
10
 
11
  # Initialize Whisper Model via Hugging Face Transformers
12
- from transformers import pipeline
13
  whisper_model = pipeline(
14
  task="automatic-speech-recognition",
15
  model="openai/whisper-small",
16
  device=0
17
  )
18
 
19
- # Set your OpenAI API Key (you should set this securely in your environment)
20
- openai.api_key = os.getenv("OPENAI_API_KEY")
21
 
22
  def detect_language(text):
23
  try:
@@ -31,7 +31,7 @@ def gpt_clean_and_translate(text, target_language):
31
  return "No text detected.", ""
32
 
33
  prompt = f"""
34
- You are an expert document reader and translator. You will receive a noisy extracted text from a government ID. Your tasks:
35
 
36
  1. Identify and extract these fields: Name, Address, Date of Birth, Expiry Date, Class, Sex.
37
  2. Output the information in full English sentences.
@@ -39,7 +39,7 @@ You are an expert document reader and translator. You will receive a noisy extra
39
  If the target language is English, just output clean English sentences.
40
  """
41
 
42
- response = openai.ChatCompletion.create(
43
  model="gpt-4o",
44
  messages=[
45
  {"role": "system", "content": prompt},
@@ -48,7 +48,7 @@ If the target language is English, just output clean English sentences.
48
  temperature=0.2
49
  )
50
 
51
- cleaned_translation = response["choices"][0]["message"]["content"].strip()
52
  return cleaned_translation
53
 
54
  def process_document(image, target_language, language_group):
 
1
  import gradio as gr
2
  from paddleocr import PaddleOCR
3
  import numpy as np
 
4
  import os
5
  from langdetect import detect
6
+ from openai import OpenAI
7
+ from transformers import pipeline
8
 
9
  # Initialize PaddleOCR
10
  ocr_reader = PaddleOCR(use_angle_cls=True, lang='en')
11
 
12
  # Initialize Whisper Model via Hugging Face Transformers
 
13
  whisper_model = pipeline(
14
  task="automatic-speech-recognition",
15
  model="openai/whisper-small",
16
  device=0
17
  )
18
 
19
+ # Initialize OpenAI Client
20
+ client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
21
 
22
  def detect_language(text):
23
  try:
 
31
  return "No text detected.", ""
32
 
33
  prompt = f"""
34
+ You are an expert document reader and translator. You will receive noisy extracted text from a government ID. Your tasks:
35
 
36
  1. Identify and extract these fields: Name, Address, Date of Birth, Expiry Date, Class, Sex.
37
  2. Output the information in full English sentences.
 
39
  If the target language is English, just output clean English sentences.
40
  """
41
 
42
+ response = client.chat.completions.create(
43
  model="gpt-4o",
44
  messages=[
45
  {"role": "system", "content": prompt},
 
48
  temperature=0.2
49
  )
50
 
51
+ cleaned_translation = response.choices[0].message.content.strip()
52
  return cleaned_translation
53
 
54
  def process_document(image, target_language, language_group):