vaseacc commited on
Commit
6a152c1
Β·
verified Β·
1 Parent(s): 6a370f9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +65 -26
app.py CHANGED
@@ -1,29 +1,68 @@
1
  import gradio as gr
2
  from transformers import pipeline
3
 
4
- # Use a smaller model that fits in 16GB RAM without quantization
5
- model = pipeline(
6
- "text-generation",
7
- model="HuggingFaceH4/zephyr-7b-beta",
8
- device_map="auto",
9
- torch_dtype="auto" # Automatically optimizes precision
10
- )
11
-
12
- def respond(prompt):
13
- try:
14
- response = model(
15
- f"<|user|>{prompt}</s><|assistant|>",
16
- max_new_tokens=40, # Shorter responses save memory
17
- do_sample=True,
18
- temperature=0.7
19
- )
20
- return response[0]['generated_text'].split("<|assistant|>")[-1].strip()
21
- except Exception as e:
22
- return f"Error: {str(e)}"
23
-
24
- gr.Interface(
25
- fn=respond,
26
- inputs=gr.Textbox(placeholder="Ask me anything..."),
27
- outputs="text",
28
- title="πŸ€– Optimized Chatbot"
29
- ).launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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()