Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,5 +1,3 @@
|
|
1 |
-
# Write the Streamlit app script
|
2 |
-
# Write the Streamlit app script
|
3 |
import streamlit as st
|
4 |
import pdfplumber
|
5 |
import torch
|
@@ -13,13 +11,8 @@ import os
|
|
13 |
|
14 |
print(os.listdir('.'))
|
15 |
|
16 |
-
|
17 |
-
# Download the 'punkt' package
|
18 |
nltk.download('punkt')
|
19 |
|
20 |
-
#openai.api_key = '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'
|
21 |
-
|
22 |
-
# Define your model architecture
|
23 |
class Bert_model(nn.Module):
|
24 |
def __init__(self, hidden_size, dropout_rate):
|
25 |
super(Bert_model, self).__init__()
|
@@ -38,22 +31,19 @@ class Bert_model(nn.Module):
|
|
38 |
logits = self.cls_final(pooled_output)
|
39 |
return logits
|
40 |
|
41 |
-
|
42 |
-
model_path = "model.pt" # Replace with your actual model path
|
43 |
state_dict = torch.load(model_path)
|
44 |
-
device = torch.device("cuda:0")
|
45 |
|
46 |
-
|
47 |
-
model = Bert_model(hidden_size=768, dropout_rate=0.1) # Adjust the hidden size to match the saved model
|
48 |
model = nn.DataParallel(model)
|
49 |
model.load_state_dict(state_dict)
|
50 |
model = model.to(device)
|
51 |
model.eval()
|
52 |
|
53 |
-
# Load the tokenizer
|
54 |
tokenizer = RobertaTokenizer.from_pretrained('deepset/roberta-base-squad2')
|
55 |
|
56 |
-
|
57 |
def preprocess_pdf(pdf_path, tokenizer):
|
58 |
with pdfplumber.open(pdf_path) as pdf:
|
59 |
text = " ".join([page.extract_text() for page in pdf.pages[2:]])
|
@@ -80,7 +70,7 @@ def translate_text(text, target_language):
|
|
80 |
|
81 |
def explain_term(term):
|
82 |
response = openai.ChatCompletion.create(
|
83 |
-
model="gpt-4",
|
84 |
messages=[
|
85 |
{
|
86 |
"role": "system",
|
@@ -95,7 +85,8 @@ def explain_term(term):
|
|
95 |
return response['choices'][0]['message']['content']
|
96 |
|
97 |
# Streamlit code to upload file
|
98 |
-
|
|
|
99 |
|
100 |
api_key = st.text_input("Enter your OpenAI API key:", type="password")
|
101 |
|
@@ -103,16 +94,14 @@ if api_key:
|
|
103 |
try:
|
104 |
openai.api_key = api_key
|
105 |
|
106 |
-
# Test the API key by making a small request
|
107 |
openai.ChatCompletion.create(
|
108 |
-
model="gpt-4",
|
109 |
messages=[
|
110 |
{"role": "system", "content": "You are a helpful assistant."},
|
111 |
{"role": "user", "content": "Hello"},
|
112 |
],
|
113 |
)
|
114 |
|
115 |
-
# If the above code doesn't raise an exception, the API key is valid
|
116 |
st.success("API key is valid!")
|
117 |
|
118 |
except Exception as e:
|
@@ -122,7 +111,6 @@ else:
|
|
122 |
|
123 |
uploaded_file = st.file_uploader("Choose a PDF file", type="pdf")
|
124 |
|
125 |
-
# Select language
|
126 |
language = st.selectbox('Select your language', ['English', 'French','Chinese','Korean','Spanish','German','Japanese'])
|
127 |
|
128 |
if uploaded_file is not None:
|
@@ -131,7 +119,6 @@ if uploaded_file is not None:
|
|
131 |
input_ids, attention_mask, text = preprocess_pdf("temp.pdf", tokenizer)
|
132 |
st.write('File successfully uploaded and processed')
|
133 |
|
134 |
-
# Ask a question
|
135 |
question = st.text_input("Enter your question:")
|
136 |
|
137 |
if question:
|
@@ -153,18 +140,10 @@ if uploaded_file is not None:
|
|
153 |
predictions.sort(key=lambda pair: pair[1], reverse=True)
|
154 |
top_5_sentences = predictions[:13]
|
155 |
|
156 |
-
#st.write("Top 5 Relevant Sentences:")
|
157 |
-
#for sentence, prediction, probabilities in top_5_sentences:
|
158 |
-
#st.write(f"Sentence: {sentence}, Prediction: {prediction}, Probability: {probabilities[prediction]}")
|
159 |
-
|
160 |
-
# Prepare the chat history with the top 3 sentences
|
161 |
chat_history = "\n".join([sentence[0] for sentence in top_5_sentences])
|
162 |
|
163 |
-
# Ask the question using OpenAI API
|
164 |
-
#openai.api_key = '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' # Replace with your actual OpenAI API key
|
165 |
-
|
166 |
response = openai.ChatCompletion.create(
|
167 |
-
model="gpt-4",
|
168 |
messages=[
|
169 |
{"role": "system", "content": "You are a helpful generator which read the short paragraphs and answer the question."},
|
170 |
{"role": "user", "content": chat_history},
|
@@ -182,7 +161,6 @@ if uploaded_file is not None:
|
|
182 |
term = st.text_input("Enter a term you want to define:")
|
183 |
|
184 |
if term:
|
185 |
-
# Define the term using OpenAI API
|
186 |
definition = explain_term(term)
|
187 |
|
188 |
if language != 'English':
|
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
import pdfplumber
|
3 |
import torch
|
|
|
11 |
|
12 |
print(os.listdir('.'))
|
13 |
|
|
|
|
|
14 |
nltk.download('punkt')
|
15 |
|
|
|
|
|
|
|
16 |
class Bert_model(nn.Module):
|
17 |
def __init__(self, hidden_size, dropout_rate):
|
18 |
super(Bert_model, self).__init__()
|
|
|
31 |
logits = self.cls_final(pooled_output)
|
32 |
return logits
|
33 |
|
34 |
+
model_path = "model.pt"
|
|
|
35 |
state_dict = torch.load(model_path)
|
36 |
+
device = torch.device("cuda:0")
|
37 |
|
38 |
+
model = Bert_model(hidden_size=768, dropout_rate=0.1)
|
|
|
39 |
model = nn.DataParallel(model)
|
40 |
model.load_state_dict(state_dict)
|
41 |
model = model.to(device)
|
42 |
model.eval()
|
43 |
|
|
|
44 |
tokenizer = RobertaTokenizer.from_pretrained('deepset/roberta-base-squad2')
|
45 |
|
46 |
+
|
47 |
def preprocess_pdf(pdf_path, tokenizer):
|
48 |
with pdfplumber.open(pdf_path) as pdf:
|
49 |
text = " ".join([page.extract_text() for page in pdf.pages[2:]])
|
|
|
70 |
|
71 |
def explain_term(term):
|
72 |
response = openai.ChatCompletion.create(
|
73 |
+
model="gpt-4.5-turbo",
|
74 |
messages=[
|
75 |
{
|
76 |
"role": "system",
|
|
|
85 |
return response['choices'][0]['message']['content']
|
86 |
|
87 |
# Streamlit code to upload file
|
88 |
+
|
89 |
+
st.title('FinChat')
|
90 |
|
91 |
api_key = st.text_input("Enter your OpenAI API key:", type="password")
|
92 |
|
|
|
94 |
try:
|
95 |
openai.api_key = api_key
|
96 |
|
|
|
97 |
openai.ChatCompletion.create(
|
98 |
+
model="gpt-4.5-turbo",
|
99 |
messages=[
|
100 |
{"role": "system", "content": "You are a helpful assistant."},
|
101 |
{"role": "user", "content": "Hello"},
|
102 |
],
|
103 |
)
|
104 |
|
|
|
105 |
st.success("API key is valid!")
|
106 |
|
107 |
except Exception as e:
|
|
|
111 |
|
112 |
uploaded_file = st.file_uploader("Choose a PDF file", type="pdf")
|
113 |
|
|
|
114 |
language = st.selectbox('Select your language', ['English', 'French','Chinese','Korean','Spanish','German','Japanese'])
|
115 |
|
116 |
if uploaded_file is not None:
|
|
|
119 |
input_ids, attention_mask, text = preprocess_pdf("temp.pdf", tokenizer)
|
120 |
st.write('File successfully uploaded and processed')
|
121 |
|
|
|
122 |
question = st.text_input("Enter your question:")
|
123 |
|
124 |
if question:
|
|
|
140 |
predictions.sort(key=lambda pair: pair[1], reverse=True)
|
141 |
top_5_sentences = predictions[:13]
|
142 |
|
|
|
|
|
|
|
|
|
|
|
143 |
chat_history = "\n".join([sentence[0] for sentence in top_5_sentences])
|
144 |
|
|
|
|
|
|
|
145 |
response = openai.ChatCompletion.create(
|
146 |
+
model="gpt-4.5-turbo",
|
147 |
messages=[
|
148 |
{"role": "system", "content": "You are a helpful generator which read the short paragraphs and answer the question."},
|
149 |
{"role": "user", "content": chat_history},
|
|
|
161 |
term = st.text_input("Enter a term you want to define:")
|
162 |
|
163 |
if term:
|
|
|
164 |
definition = explain_term(term)
|
165 |
|
166 |
if language != 'English':
|