josequinonez commited on
Commit
f97afaf
·
verified ·
1 Parent(s): 25f6eeb

Upload 2 files

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