Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,46 +1,22 @@
|
|
1 |
-
import gradio as gr
|
2 |
import pandas as pd
|
3 |
-
|
4 |
-
import
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
tokenizer.pad_token = tokenizer.eos_token
|
25 |
-
model = GPT2LMHeadModel.from_pretrained('distilgpt2')
|
26 |
-
|
27 |
-
def setup_gradio_interface():
|
28 |
-
with gr.Blocks() as demo:
|
29 |
-
gr.Markdown("### Upload emails.csv and Ask a Question")
|
30 |
-
with gr.Row():
|
31 |
-
file_input = gr.File(label="Upload your emails.csv")
|
32 |
-
submit_file = gr.Button("Load & Process File")
|
33 |
-
data_output = gr.Textbox(label="File Processing Output")
|
34 |
-
question_input = gr.Textbox(label="Enter your question:")
|
35 |
-
answer_output = gr.Textbox(label="Model Answer:")
|
36 |
-
submit_question = gr.Button("Get Answer")
|
37 |
-
|
38 |
-
submit_file.click(fn=load_and_process_data, inputs=file_input, outputs=[data_output])
|
39 |
-
submit_question.click(fn=answer_question, inputs=[model, tokenizer, question_input], outputs=answer_output)
|
40 |
-
|
41 |
-
return demo
|
42 |
-
|
43 |
-
demo = setup_gradio_interface()
|
44 |
-
|
45 |
-
if __name__ == "__main__":
|
46 |
-
demo.launch()
|
|
|
|
|
1 |
import pandas as pd
|
2 |
+
import gradio as gr
|
3 |
+
from transformers import pipeline
|
4 |
+
|
5 |
+
emails_df = pd.read_csv('emails.csv', nrows=500, on_bad_lines='skip')
|
6 |
+
context = " ".join(emails_df['message'].apply(lambda x: x.strip() if isinstance(x, str) else ''))
|
7 |
+
|
8 |
+
qa_pipeline = pipeline("question-answering")
|
9 |
+
|
10 |
+
def answer_query(question):
|
11 |
+
try:
|
12 |
+
result = qa_pipeline(question=question, context=context)
|
13 |
+
return result['answer']
|
14 |
+
except Exception as e:
|
15 |
+
return str(e)
|
16 |
+
|
17 |
+
iface = gr.Interface(
|
18 |
+
fn=answer_query,
|
19 |
+
inputs=gr.inputs.Textbox(label="Enter your question:"),
|
20 |
+
outputs=gr.outputs.Textbox(label="Answer:")
|
21 |
+
)
|
22 |
+
iface.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|