Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
@@ -1,29 +1,68 @@
|
|
1 |
import gradio as gr
|
2 |
from transformers import pipeline
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
from transformers import pipeline
|
3 |
|
4 |
+
# --- 1. LOAD OUR AI MODELS ---
|
5 |
+
# We load the models once when the app starts.
|
6 |
+
# This is more efficient than loading them for every request.
|
7 |
+
question_answerer = pipeline("question-answering", model="distilbert-base-uncased-distilled-squad")
|
8 |
+
summarizer = pipeline("summarization", model="sshleifer/distilbart-cnn-6-6")
|
9 |
+
|
10 |
+
|
11 |
+
# --- 2. DEFINE THE CORE FUNCTIONS ---
|
12 |
+
# These functions will take user input and use the appropriate model.
|
13 |
+
|
14 |
+
def answer_question(context, question):
|
15 |
+
"""Uses the QA model to find an answer within a given text."""
|
16 |
+
if not context or not question:
|
17 |
+
return "(Please provide both context and a question.)"
|
18 |
+
result = question_answerer(question=question, context=context)
|
19 |
+
return result['answer']
|
20 |
+
|
21 |
+
def summarize_text(text):
|
22 |
+
"""Uses the Summarization model to shorten a piece of text."""
|
23 |
+
if not text:
|
24 |
+
return "(Please provide text to summarize.)"
|
25 |
+
# We add some parameters for better, shorter summaries on a CPU
|
26 |
+
summary = summarizer(text, max_length=50, min_length=25, do_sample=False)
|
27 |
+
return summary[0]['summary_text']
|
28 |
+
|
29 |
+
|
30 |
+
# --- 3. BUILD THE GRADIO INTERFACE ---
|
31 |
+
# This is where we design the web app's layout and connect it to our functions.
|
32 |
+
|
33 |
+
with gr.Blocks() as demo:
|
34 |
+
gr.Markdown("# Ultimate AI Assistant (CPU Edition) π€")
|
35 |
+
gr.Markdown("An experiment in combining multiple small, efficient AI models into one application.")
|
36 |
+
|
37 |
+
with gr.Tabs():
|
38 |
+
# --- First Tab: Question Answering ---
|
39 |
+
with gr.TabItem("β Ask a Question"):
|
40 |
+
gr.Markdown("Give the AI some text (context) and ask a question about it.")
|
41 |
+
with gr.Row():
|
42 |
+
qa_context_input = gr.Textbox(lines=7, label="Context", placeholder="Paste a paragraph or article here...")
|
43 |
+
qa_question_input = gr.Textbox(label="Question", placeholder="What do you want to know?")
|
44 |
+
qa_button = gr.Button("Get Answer")
|
45 |
+
qa_output = gr.Textbox(label="Answer")
|
46 |
+
|
47 |
+
# --- Second Tab: Summarization ---
|
48 |
+
with gr.TabItem("π Summarize Text"):
|
49 |
+
gr.Markdown("Paste in a long piece of text and the AI will create a short summary.")
|
50 |
+
summarize_input = gr.Textbox(lines=10, label="Text to Summarize", placeholder="Paste a long article or text here...")
|
51 |
+
summarize_button = gr.Button("Summarize")
|
52 |
+
summarize_output = gr.Textbox(label="Summary")
|
53 |
+
|
54 |
+
# --- 4. Connect Buttons to Functions ---
|
55 |
+
qa_button.click(
|
56 |
+
fn=answer_question,
|
57 |
+
inputs=[qa_context_input, qa_question_input],
|
58 |
+
outputs=qa_output
|
59 |
+
)
|
60 |
+
|
61 |
+
summarize_button.click(
|
62 |
+
fn=summarize_text,
|
63 |
+
inputs=summarize_input,
|
64 |
+
outputs=summarize_output
|
65 |
+
)
|
66 |
+
|
67 |
+
# --- 5. LAUNCH THE APP! ---
|
68 |
+
demo.launch()
|