Spaces:
Sleeping
Sleeping
mohammed3536
commited on
Commit
•
93a5e08
1
Parent(s):
1bb215c
Update app.py
Browse files
app.py
CHANGED
@@ -1,18 +1,14 @@
|
|
1 |
import PyPDF2
|
2 |
import nltk
|
3 |
-
from nltk.tokenize import sent_tokenize
|
4 |
import random
|
5 |
-
import requests
|
6 |
import streamlit as st
|
7 |
from langchain_openai import OpenAI
|
8 |
|
9 |
# Download NLTK data (if not already downloaded)
|
10 |
nltk.download('punkt')
|
11 |
-
nltk.download('averaged_perceptron_tagger')
|
12 |
|
13 |
-
#
|
14 |
-
|
15 |
-
OPENAI_API_KEY = "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"
|
16 |
|
17 |
def extract_text_from_pdf(pdf_file):
|
18 |
pdf_reader = PyPDF2.PdfReader(pdf_file)
|
@@ -31,19 +27,12 @@ def generate_mcqs_on_topic(text, topic, num_mcqs=5):
|
|
31 |
mcqs = []
|
32 |
for sentence in selected_sentences:
|
33 |
# Use ChatGPT for interactive question generation
|
34 |
-
chatgpt_question = generate_question_with_chatgpt(sentence)
|
35 |
mcqs.append(chatgpt_question)
|
36 |
|
37 |
return mcqs
|
38 |
|
39 |
-
def generate_question_with_chatgpt(context):
|
40 |
-
|
41 |
-
|
42 |
-
headers = {
|
43 |
-
"Content-Type": "application/json",
|
44 |
-
"Authorization": f"Bearer {OPENAI_API_KEY}",
|
45 |
-
}
|
46 |
-
|
47 |
# Initializing the default value
|
48 |
generated_question = {
|
49 |
'content': "Unable to generate a question..",
|
@@ -57,11 +46,11 @@ def generate_question_with_chatgpt(context):
|
|
57 |
"max_tokens": 1024,
|
58 |
"messages": [
|
59 |
{"role": "system", "content": "You are a helpful assistant."},
|
60 |
-
{"role": "user", "content": f"What is the question for the following? {context}"},
|
61 |
],
|
62 |
}
|
63 |
|
64 |
-
response =
|
65 |
result = response.json()
|
66 |
|
67 |
print("API Response:", result) # Add this line for debugging
|
@@ -78,7 +67,6 @@ def generate_question_with_chatgpt(context):
|
|
78 |
|
79 |
return generated_question
|
80 |
|
81 |
-
|
82 |
def main():
|
83 |
# Title of the Application
|
84 |
st.header("🤖CB Quiz Generator🧠", divider='rainbow')
|
|
|
1 |
import PyPDF2
|
2 |
import nltk
|
|
|
3 |
import random
|
|
|
4 |
import streamlit as st
|
5 |
from langchain_openai import OpenAI
|
6 |
|
7 |
# Download NLTK data (if not already downloaded)
|
8 |
nltk.download('punkt')
|
|
|
9 |
|
10 |
+
# LangChain OpenAI wrapper
|
11 |
+
openai = OpenAI(api_key="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")
|
|
|
12 |
|
13 |
def extract_text_from_pdf(pdf_file):
|
14 |
pdf_reader = PyPDF2.PdfReader(pdf_file)
|
|
|
27 |
mcqs = []
|
28 |
for sentence in selected_sentences:
|
29 |
# Use ChatGPT for interactive question generation
|
30 |
+
chatgpt_question = generate_question_with_chatgpt(sentence, topic)
|
31 |
mcqs.append(chatgpt_question)
|
32 |
|
33 |
return mcqs
|
34 |
|
35 |
+
def generate_question_with_chatgpt(context, topic):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
36 |
# Initializing the default value
|
37 |
generated_question = {
|
38 |
'content': "Unable to generate a question..",
|
|
|
46 |
"max_tokens": 1024,
|
47 |
"messages": [
|
48 |
{"role": "system", "content": "You are a helpful assistant."},
|
49 |
+
{"role": "user", "content": f"What is the question on {topic} for the following? {context}"},
|
50 |
],
|
51 |
}
|
52 |
|
53 |
+
response = openai.chat.completions.create(data)
|
54 |
result = response.json()
|
55 |
|
56 |
print("API Response:", result) # Add this line for debugging
|
|
|
67 |
|
68 |
return generated_question
|
69 |
|
|
|
70 |
def main():
|
71 |
# Title of the Application
|
72 |
st.header("🤖CB Quiz Generator🧠", divider='rainbow')
|