Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
|
| 3 |
+
import openai
|
| 4 |
+
import gradio as gr
|
| 5 |
+
import pytesseract
|
| 6 |
+
from PIL import Image
|
| 7 |
+
import numpy as np
|
| 8 |
+
|
| 9 |
+
# set your OpenAI API key here
|
| 10 |
+
openai.api_key = ""
|
| 11 |
+
|
| 12 |
+
def generate_questions(text):
|
| 13 |
+
# set parameters for question generation
|
| 14 |
+
prompts = [
|
| 15 |
+
f"Generate 5-mark questions based on the following text:\n{text}\n\nQuestions:",
|
| 16 |
+
f"What are some opinion-based questions that can be asked about the following text?\n{text}\n\nQuestions:",
|
| 17 |
+
f"What are some fact-based questions that can be asked about the following text?\n{text}\n\nQuestions:",
|
| 18 |
+
f"Generate 3 tricky questions that require creative thinking based on the following text:\n{text}\n\nQuestions:",
|
| 19 |
+
f"Generate 2 long questions (10+ words) and 3 short questions (5-7 words) based on the following text:\n{text}\n\nQuestions:"
|
| 20 |
+
]
|
| 21 |
+
model = "text-davinci-003"
|
| 22 |
+
temperature = 0.7
|
| 23 |
+
max_tokens = 200
|
| 24 |
+
|
| 25 |
+
# generate questions using the OpenAI API
|
| 26 |
+
questions = []
|
| 27 |
+
counter = 1 # initialize counter variable
|
| 28 |
+
for prompt in prompts:
|
| 29 |
+
response = openai.Completion.create(engine=model, prompt=prompt, temperature=temperature, max_tokens=max_tokens)
|
| 30 |
+
prompt_questions = response.choices[0].text.strip().split("\n")
|
| 31 |
+
# remove any duplicate questions
|
| 32 |
+
prompt_questions = list(set(prompt_questions))
|
| 33 |
+
for question in prompt_questions:
|
| 34 |
+
numbered_question = f"{counter}. {question}" # add counter to beginning of question
|
| 35 |
+
questions.append(numbered_question)
|
| 36 |
+
counter += 1
|
| 37 |
+
|
| 38 |
+
# return the generated questions
|
| 39 |
+
return "\n".join(questions)
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
def generate_questions_from_image(image):
|
| 43 |
+
# preprocess image and convert to text
|
| 44 |
+
image = Image.fromarray(np.uint8(image))
|
| 45 |
+
image_text = pytesseract.image_to_string(image)
|
| 46 |
+
|
| 47 |
+
# generate questions from image text
|
| 48 |
+
return generate_questions(image_text)
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
# create the Gradio interface
|
| 52 |
+
iface = gr.Interface(fn=generate_questions_from_image,
|
| 53 |
+
inputs=gr.inputs.Image(label="Upload an image"),
|
| 54 |
+
outputs=gr.outputs.Textbox(label="Generated Questions"),
|
| 55 |
+
title="Question Generator By Daksh Chawla",
|
| 56 |
+
description="Generate a diverse set of questions based on the provided image input.")
|
| 57 |
+
|
| 58 |
+
# launch the interface
|
| 59 |
+
iface.launch(share=True)
|
| 60 |
+
|