-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStreamlitApp.py
More file actions
97 lines (79 loc) · 3.79 KB
/
StreamlitApp.py
File metadata and controls
97 lines (79 loc) · 3.79 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import os
import json
import traceback
import pandas as pd
from dotenv import load_dotenv
from src.mcqgenerator.utils import read_file, get_table_data
import streamlit as st
from langchain.callbacks import get_openai_callback
from src.mcqgenerator.MCQGenerator import generate_evaluate_chain
from src.mcqgenerator.logger import logging
# Load environment variables
load_dotenv()
# Loading JSON file
try:
with open('C:/Users/ACER/Gen1Project/response.json', 'r') as file:
RESPONSE_JSON = json.load(file)
# Further code using RESPONSE_JSON here
#st.write(RESPONSE_JSON)
st.title("MCQs Creator Application with LangChain ($8)")
# Create a form using st.form
with st.form("user_inputs"):
# File Upload
uploaded_file = st.file_uploader("Upload a PDF or txt file")
# Input Fields
mcq_count = st.number_input("No. of MCQs", min_value=3, max_value=50)
# Subject Input
subject = st.text_input("Insert Subject", max_chars=20)
# Quiz Tone (Complexity Level)
tone = st.text_input("Complexity Level Of Questions", max_chars=20, placeholder="Simple")
# Submit button for the form
button = st.form_submit_button("Create MCQs")
if button and uploaded_file is not None and mcq_count and subject and tone:
with st.spinner("Loading..."):
try:
# Read file content
text = read_file(uploaded_file)
# Count tokens and the cost of API call using LangChain callback
with get_openai_callback() as cb:
# Call the function to generate MCQs
response = generate_evaluate_chain(
text=text,
number=mcq_count,
subject=subject,
tone=tone,
response_json=json.dumps(RESPONSE_JSON)
)
# Display the response (MCQs generated)
st.write(response)
except Exception as e:
# Handle exceptions and display the error
st.error("An error occurred while generating MCQs.")
traceback.print_exception(type(e), e, e.__traceback__)
st.error("Error")
else:
# Display token and cost details
st.write(f"Total Tokens: {cb.total_tokens}")
st.write(f"Prompt Tokens: {cb.prompt_tokens}")
st.write(f"Completion Tokens: {cb.completion_tokens}")
st.write(f"Total Cost: {cb.total_cost}")
# Check if response is a dictionary and extract quiz data
if isinstance(response, dict):
quiz = response.get("quiz", None)
if quiz is not None:
table_data = get_table_data(quiz)
if table_data is not None:
# Convert table data to a DataFrame
df = pd.DataFrame(table_data)
df.index = df.index + 1
# Display the table in Streamlit
st.table(df)
# Display the review in a text area
st.text_area(label="Review", value=response.get("review", "No review available"))
else:
st.error("Error in the table data.")
else:
st.write(response)
except Exception as e:
st.error("An error occurred while loading the JSON file.")
traceback.print_exception(type(e), e, e.__traceback__)