Spaces:
Runtime error
Runtime error
app file
Browse files
daksh.py
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import openai
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import re
|
| 4 |
+
import pytesseract
|
| 5 |
+
from PIL import Image
|
| 6 |
+
|
| 7 |
+
# Set up the OpenAI API key
|
| 8 |
+
openai.api_key = "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 "
|
| 9 |
+
|
| 10 |
+
def classify_words(text):
|
| 11 |
+
# Use the OpenAI GPT-3 model to identify hard English words in the input text
|
| 12 |
+
response = openai.Completion.create(
|
| 13 |
+
engine="text-davinci-003",
|
| 14 |
+
prompt=f"Identify hard English words in the following text and provide their simple English meanings:\n\n{text}\n\nHard words:",
|
| 15 |
+
max_tokens=1000,
|
| 16 |
+
n=1,
|
| 17 |
+
stop=None,
|
| 18 |
+
temperature=0.5,
|
| 19 |
+
)
|
| 20 |
+
|
| 21 |
+
# Parse the OpenAI API response to extract the identified hard English words and their simple English meanings
|
| 22 |
+
output = response.choices[0].text.strip()
|
| 23 |
+
hard_words_list = re.findall(r"([\w'-]+):\s(.+)", output)
|
| 24 |
+
hard_words_dict = dict(hard_words_list)
|
| 25 |
+
|
| 26 |
+
# Generate the output text with the identified hard English words and their simple English meanings
|
| 27 |
+
output_text = "Identified hard English words and their simple English meanings:\n\n"
|
| 28 |
+
for word, meaning in hard_words_dict.items():
|
| 29 |
+
output_text += f"{word}: {meaning}\n"
|
| 30 |
+
|
| 31 |
+
return output_text
|
| 32 |
+
|
| 33 |
+
def image_to_text(file):
|
| 34 |
+
# Use Tesseract OCR to extract text from the input image
|
| 35 |
+
text = pytesseract.image_to_string(Image.open(file.name))
|
| 36 |
+
return text
|
| 37 |
+
|
| 38 |
+
# Set up the Gradio interface
|
| 39 |
+
input_text = gr.inputs.Textbox(label="Enter a paragraph of text with hard English words")
|
| 40 |
+
input_image = gr.inputs.Image(label="Upload an image containing text")
|
| 41 |
+
output_text = gr.outputs.Textbox(label="Simple English meanings of identified hard English words")
|
| 42 |
+
|
| 43 |
+
gr.Interface(classify_words, inputs=[input_text, input_image], outputs=output_text, title="Hard English Word Classifier", description="Enter a paragraph of text with hard English words or upload an image containing text to identify and provide their simple English meanings.").launch()
|