Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,77 +1,48 @@
|
|
1 |
-
import
|
2 |
-
import
|
3 |
-
|
4 |
-
import
|
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 |
-
def clarify_concept(question):
|
51 |
-
global extracted_text
|
52 |
-
if not extracted_text:
|
53 |
-
return "Please upload and extract text from a PPTX file first."
|
54 |
-
prompt = f"Context:\n{extracted_text}\n\nQuestion: {question}"
|
55 |
-
response = gemini_model.generate_content(prompt)
|
56 |
-
return response.text if response else "No response from Gemini."
|
57 |
-
|
58 |
-
with gr.Blocks() as demo:
|
59 |
-
gr.Markdown("## π§ AI-Powered Study Assistant for PowerPoint Lectures")
|
60 |
-
|
61 |
-
pptx_input = gr.File(label="π Upload PPTX File", file_types=[".pptx"])
|
62 |
-
extract_btn = gr.Button("π Extract & Summarize")
|
63 |
-
|
64 |
-
extracted_output = gr.Textbox(label="π Extracted Text", lines=10, interactive=False)
|
65 |
-
summary_output = gr.Textbox(label="π Summary", interactive=False)
|
66 |
-
|
67 |
-
extract_btn.click(handle_pptx_upload, inputs=[pptx_input], outputs=[extracted_output])
|
68 |
-
extract_btn.click(summarize_text, outputs=[summary_output])
|
69 |
-
|
70 |
-
question = gr.Textbox(label="β Ask a Question")
|
71 |
-
ask_btn = gr.Button("π¬ Ask Gemini")
|
72 |
-
ai_answer = gr.Textbox(label="π€ Gemini Answer", lines=4)
|
73 |
-
|
74 |
-
ask_btn.click(clarify_concept, inputs=[question], outputs=[ai_answer])
|
75 |
-
|
76 |
-
if __name__ == "__main__":
|
77 |
-
demo.launch()
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
|
3 |
+
import PyPDF2
|
4 |
+
import torch
|
5 |
+
|
6 |
+
st.set_page_config(page_title="Perplexity Clone (Gemma)", layout="wide")
|
7 |
+
st.title("π Perplexity-Style AI Study Assistant using Gemma")
|
8 |
+
|
9 |
+
# Load Gemma model and tokenizer
|
10 |
+
@st.cache_resource
|
11 |
+
def load_model():
|
12 |
+
tokenizer = AutoTokenizer.from_pretrained("google/gemma-7b-it")
|
13 |
+
model = AutoModelForCausalLM.from_pretrained(
|
14 |
+
"google/gemma-7b-it",
|
15 |
+
torch_dtype=torch.float16,
|
16 |
+
device_map="auto"
|
17 |
+
)
|
18 |
+
pipe = pipeline("text-generation", model=model, tokenizer=tokenizer, max_new_tokens=512)
|
19 |
+
return pipe
|
20 |
+
|
21 |
+
textgen = load_model()
|
22 |
+
|
23 |
+
# Extract text from uploaded PDF
|
24 |
+
def extract_text_from_pdf(file):
|
25 |
+
reader = PyPDF2.PdfReader(file)
|
26 |
+
text = ""
|
27 |
+
for page in reader.pages:
|
28 |
+
text += page.extract_text() + "\n"
|
29 |
+
return text.strip()
|
30 |
+
|
31 |
+
# UI Layout
|
32 |
+
query = st.text_input("Ask a question or type a query:")
|
33 |
+
|
34 |
+
uploaded_file = st.file_uploader("Or upload a PDF to analyze its content:", type=["pdf"])
|
35 |
+
|
36 |
+
context = ""
|
37 |
+
if uploaded_file:
|
38 |
+
context = extract_text_from_pdf(uploaded_file)
|
39 |
+
st.text_area("Extracted Content", context, height=200)
|
40 |
+
|
41 |
+
if st.button("Generate Answer"):
|
42 |
+
with st.spinner("Generating with Gemma..."):
|
43 |
+
prompt = query
|
44 |
+
if context:
|
45 |
+
prompt = f"Context:\n{context}\n\nQuestion: {query}"
|
46 |
+
output = textgen(prompt)[0]["generated_text"]
|
47 |
+
st.success("Answer:")
|
48 |
+
st.write(output)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|