dc07's picture
Update app.py
afcfb11
Raw History Blame Contribute Delete
2.51 kB
## install PyTesseract
os.system('pip install -q pytesseract')
import openai
import gradio as gr
import pytesseract
from PIL import Image
import numpy as np
# set your OpenAI API key here
openai.api_key = "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"
def generate_questions(text):
# set parameters for question generation
prompts = [
f"Generate 5-mark questions based on the following text:\n{text}\n\nQuestions:",
f"What are some opinion-based questions that can be asked about the following text?\n{text}\n\nQuestions:",
f"What are some fact-based questions that can be asked about the following text?\n{text}\n\nQuestions:",
f"Generate 3 tricky questions that require creative thinking based on the following text:\n{text}\n\nQuestions:",
f"Generate 2 long questions (10+ words) and 3 short questions (5-7 words) based on the following text:\n{text}\n\nQuestions:"
]
model = "text-davinci-003"
temperature = 0.7
max_tokens = 200
# generate questions using the OpenAI API
questions = []
counter = 1 # initialize counter variable
for prompt in prompts:
response = openai.Completion.create(engine=model, prompt=prompt, temperature=temperature, max_tokens=max_tokens)
prompt_questions = response.choices[0].text.strip().split("\n")
# remove any duplicate questions
prompt_questions = list(set(prompt_questions))
for question in prompt_questions:
numbered_question = f"{counter}. {question}" # add counter to beginning of question
questions.append(numbered_question)
counter += 1
# return the generated questions
return "\n".join(questions)
def generate_questions_from_image(image):
# preprocess image and convert to text
image = Image.fromarray(np.uint8(image))
image_text = pytesseract.image_to_string(image)
# generate questions from image text
return generate_questions(image_text)
# create the Gradio interface
iface = gr.Interface(fn=generate_questions_from_image,
inputs=gr.inputs.Image(label="Upload an image"),
outputs=gr.outputs.Textbox(label="Generated Questions"),
title="Question Generator By Daksh Chawla",
description="Generate a diverse set of questions based on the provided image input.")
# launch the interface
iface.launch(share=False)