Baolina commited on
Commit
ce52a63
·
verified ·
1 Parent(s): 2c7d8df

Upload 3 files

Browse files
Files changed (3) hide show
  1. Dockerfile +44 -0
  2. app.py +109 -0
  3. requirements.txt +7 -0
Dockerfile ADDED
@@ -0,0 +1,44 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ # Use an official Python runtime as a parent image
3
+ FROM python:3.10-slim
4
+
5
+
6
+ # Set environment variables
7
+ ENV PYTHONUNBUFFERED 1
8
+
9
+
10
+ # Install system dependencies and git
11
+ RUN apt-get update && apt-get install -y \
12
+ build-essential \
13
+ git \
14
+ && rm -rf /var/lib/apt/lists/*
15
+
16
+
17
+
18
+
19
+ # Create a non-root user and set permissions
20
+ RUN useradd -ms /bin/bash appuser
21
+ # Set the working directory in the container
22
+ WORKDIR /home/appuser/app
23
+
24
+
25
+ # Copy the requirements file and install dependencies
26
+ COPY requirements.txt .
27
+ RUN pip install --upgrade pip && pip install -r requirements.txt
28
+
29
+
30
+ # Switch to non-root user
31
+ USER appuser
32
+
33
+
34
+ # Copy the rest of the application code into the container
35
+ COPY --chown=appuser . /home/appuser/app
36
+
37
+
38
+ # Expose the port that the app runs on
39
+ EXPOSE 8501
40
+
41
+
42
+ # Command to run the application
43
+ CMD ["streamlit", "run", "app.py", "--server.port=8501", "--server.address=0.0.0.0"]
44
+
app.py ADDED
@@ -0,0 +1,109 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ import streamlit as st
3
+ from transformers import AutoModelForCausalLM, AutoTokenizer
4
+ import torch
5
+ import os
6
+ from huggingface_hub import login
7
+
8
+
9
+ model_name_tinyllama = "TinyLlama/TinyLlama-1.1B-Chat-v1.0"
10
+ tokenizer_tinyllama = AutoTokenizer.from_pretrained(model_name_tinyllama)
11
+ model_tinyllama = AutoModelForCausalLM.from_pretrained(model_name_tinyllama,torch_dtype=torch.float32,device_map={"": "cpu"})
12
+
13
+ def summarize_tinyllama(article):
14
+ # For causal models like TinyLlama, summarization isn't a direct task like with encoder-decoder models.
15
+ # We can prompt it to continue a summary.
16
+ prompt="Summarize the following article clearly and concisely:"
17
+ input_text = f"{prompt}\n{article}\nSummary:"
18
+ inputs = tokenizer_tinyllama(input_text, return_tensors="pt", max_length=1024, truncation=True)
19
+
20
+ # Generate tokens - the model will try to complete the input prompt.
21
+ # We need to adjust generation parameters for open-ended generation.
22
+ # max_new_tokens controls how much new text is generated after the prompt.
23
+ outputs = model_tinyllama.generate(
24
+ inputs["input_ids"],
25
+ attention_mask=inputs["attention_mask"],
26
+ max_new_tokens=500, # Generate up to 300 new tokens for the summary
27
+ do_sample=True, # Don't sample, use greedy decoding
28
+ temperature=0.7,
29
+ min_new_tokens=150,
30
+ top_p=0.9,
31
+ pad_token_id=tokenizer_tinyllama.eos_token_id, # Pad with EOS token if needed
32
+ )
33
+
34
+ # Decode the entire output sequence.
35
+ generated_text = tokenizer_tinyllama.decode(outputs[0], skip_special_tokens=True)
36
+
37
+
38
+ # The generated text will include the original prompt. We need to extract the summary part.
39
+ # This is a simple approach, more sophisticated parsing might be needed depending on prompt and output.
40
+ summary_start_index = generated_text.find("Summary:") + len("Summary:")
41
+ summary = generated_text[summary_start_index:].strip()
42
+
43
+ return summary
44
+
45
+ def answer_question_tinyllama(article, question):
46
+ # Formulate the prompt to guide the TinyLlama model to answer the question based on the article.
47
+ # We ask the model to act as an AI answering a question based on the provided text.
48
+ input_text = f"From this Article: {article}\n\n Answer the below Question: {question}\n\nAnswer:"
49
+
50
+ # Tokenize the input text
51
+ # Truncate if the combined article and question is too long
52
+ inputs = tokenizer_tinyllama(input_text, return_tensors="pt", max_length=1024, truncation=True)
53
+
54
+ # Generate the answer using the model.
55
+ # We use generate with parameters suitable for generating a concise answer.
56
+ outputs = model_tinyllama.generate(
57
+ inputs["input_ids"],
58
+ attention_mask=inputs["attention_mask"],
59
+ max_new_tokens=500, # Generate up to 100 new tokens for the answer
60
+ do_sample=True, # Use sampling to potentially get more varied answers
61
+ temperature=0.7, # Control randomness
62
+ top_p=0.9, # Nucleus sampling
63
+ pad_token_id=tokenizer_tinyllama.eos_token_id, # Pad with EOS token if needed
64
+ )
65
+ # Decode the generated sequence
66
+ generated_text = tokenizer_tinyllama.decode(outputs[0], skip_special_tokens=True)
67
+
68
+ # The generated text will include the original prompt. We need to extract the answer part.
69
+ # This is a simple approach, more sophisticated parsing might be needed depending on prompt and output.
70
+ answer_start_index = generated_text.find("Answer:") + len("Answer:")
71
+ answer = generated_text[answer_start_index:].strip()
72
+
73
+ # Basic cleanup: remove potential repetition of the question or prompt in the answer
74
+ if answer.startswith(question):
75
+ answer = answer[len(question):].strip()
76
+
77
+ return answer
78
+
79
+
80
+ st.title("Smart Article Insights Generator")
81
+ st.markdown("Summarize an article or ask a question about it.")
82
+
83
+ mode = st.radio("Select Mode", ["Summarize", "Answer Question"])
84
+
85
+ article_input = st.text_area("Article Text", height=300, placeholder="Paste the article here...")
86
+
87
+ question_input = None
88
+ if mode == "Answer Question":
89
+ question_input = st.text_input("Question", placeholder="Enter your question here...")
90
+
91
+ if st.button("Process"):
92
+ if mode == "Summarize":
93
+ if article_input:
94
+ with st.spinner("Generating summary..."):
95
+ output = summarize_tinyllama(article_input)
96
+ st.subheader("Summary")
97
+ st.write(output)
98
+ else:
99
+ st.warning("Please provide an article to summarize.")
100
+ elif mode == "Answer Question":
101
+ if article_input and question_input:
102
+ with st.spinner("Generating answer..."):
103
+ output = answer_question_tinyllama(article_input, question_input)
104
+ st.subheader("Answer")
105
+ st.write(output)
106
+ elif not article_input:
107
+ st.warning("Please provide an article to answer the question from.")
108
+ elif not question_input:
109
+ st.warning("Please provide a question to answer.")
requirements.txt ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ numpy==1.26.4
2
+ transformers==4.53.1
3
+ pandas==2.2.2
4
+ streamlit==1.47.0
5
+ torch==2.9.0
6
+ accelerate==0.30.0
7
+ huggingface_hub==0.36.0