Spaces:
Runtime error
Runtime error
File size: 2,514 Bytes
afcfb11 5977f2d dfb1aa2 afcfb11 dfb1aa2 4aed0cf | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | ## 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)
|