Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files- app.py +94 -0
- requirements.txt +9 -0
app.py
ADDED
@@ -0,0 +1,94 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from langchain.prompts import PromptTemplate
|
3 |
+
from langchain.llms import CTransformers
|
4 |
+
import os
|
5 |
+
from huggingface_hub import hf_hub_download
|
6 |
+
|
7 |
+
|
8 |
+
# Function to download the LLaMA model
|
9 |
+
def download_model():
|
10 |
+
model_path = "models/llama-2-7b-chat.ggmlv3.q8_0.bin"
|
11 |
+
if not os.path.exists(model_path):
|
12 |
+
model_path = hf_hub_download(repo_id="TheBloke/Llama-2-7B-Chat-GGML",
|
13 |
+
filename="llama-2-7b-chat.ggmlv3.q8_0.bin",
|
14 |
+
local_dir="models")
|
15 |
+
return model_path
|
16 |
+
|
17 |
+
|
18 |
+
# Function to get LLaMA response
|
19 |
+
def getLLamaresponse(input_text, no_words, blog_style):
|
20 |
+
model_path = download_model()
|
21 |
+
|
22 |
+
llm = CTransformers(model=model_path,
|
23 |
+
model_type='llama',
|
24 |
+
config={'max_new_tokens': 256, 'temperature': 0.01})
|
25 |
+
|
26 |
+
# Blog prompt template
|
27 |
+
template = """
|
28 |
+
Write a blog for {blog_style} job profile for a topic {input_text}
|
29 |
+
within {no_words} words.
|
30 |
+
"""
|
31 |
+
|
32 |
+
prompt = PromptTemplate(input_variables=["blog_style", "input_text", "no_words"],
|
33 |
+
template=template)
|
34 |
+
|
35 |
+
# Generate response
|
36 |
+
response = llm(prompt.format(blog_style=blog_style, input_text=input_text, no_words=no_words))
|
37 |
+
return response
|
38 |
+
|
39 |
+
|
40 |
+
# --------------- Streamlit UI Design ----------------
|
41 |
+
st.set_page_config(page_title="AI Blog Generator", page_icon="π", layout="centered")
|
42 |
+
|
43 |
+
# Custom CSS for a clean UI
|
44 |
+
st.markdown("""
|
45 |
+
<style>
|
46 |
+
.main {background-color: #f4f4f4;}
|
47 |
+
.stTextInput, .stSelectbox, .stNumberInput {width: 100%;}
|
48 |
+
.stButton>button {width: 100%; background-color: #4CAF50; color: white; padding: 10px 20px; font-size: 18px; border-radius: 8px;}
|
49 |
+
.stButton>button:hover {background-color: #45a049;}
|
50 |
+
</style>
|
51 |
+
""", unsafe_allow_html=True)
|
52 |
+
|
53 |
+
# Page Title
|
54 |
+
st.markdown("<h1 style='text-align: center; color: #333;'>π AI Blog Generator</h1>", unsafe_allow_html=True)
|
55 |
+
st.markdown("<h3 style='text-align: center; color: #666;'>Generate high-quality blogs instantly with LLaMA 2</h3>",
|
56 |
+
unsafe_allow_html=True)
|
57 |
+
st.write("---")
|
58 |
+
|
59 |
+
# Sidebar
|
60 |
+
with st.sidebar:
|
61 |
+
|
62 |
+
st.markdown("## π₯ About This App")
|
63 |
+
st.write("This AI-powered app generates professional blogs using **LLaMA 2** from Meta.")
|
64 |
+
st.markdown("#### β¨ Features")
|
65 |
+
st.write("- Generates **high-quality** blog content.")
|
66 |
+
st.write("- **Custom word count** & writing style.")
|
67 |
+
st.write("- **Powered by LLaMA 2 (7B)** model.")
|
68 |
+
st.write("---")
|
69 |
+
st.write("π [Hugging Face Model](https://huggingface.co/TheBloke/Llama-2-7B-Chat-GGML)")
|
70 |
+
|
71 |
+
# Input Fields
|
72 |
+
st.markdown("### βοΈ Enter Blog Details")
|
73 |
+
input_text = st.text_input("π Blog Topic", placeholder="e.g. Future of AI in Healthcare")
|
74 |
+
|
75 |
+
col1, col2 = st.columns([5, 5])
|
76 |
+
with col1:
|
77 |
+
no_words = st.number_input('π Word Count', min_value=50, max_value=1000, value=300, step=50)
|
78 |
+
with col2:
|
79 |
+
blog_style = st.selectbox("βοΈ Target Audience", ("Researchers", "Data Scientist", "Common People"), index=0)
|
80 |
+
|
81 |
+
# Generate Button
|
82 |
+
submit = st.button("π Generate Blog")
|
83 |
+
|
84 |
+
if submit:
|
85 |
+
if not input_text:
|
86 |
+
st.error("β οΈ Please enter a blog topic.")
|
87 |
+
else:
|
88 |
+
with st.spinner("β³ Generating your blog..."):
|
89 |
+
blog_content = getLLamaresponse(input_text, no_words, blog_style)
|
90 |
+
st.success("β
Blog Generated Successfully!")
|
91 |
+
|
92 |
+
# Display the blog
|
93 |
+
st.markdown("### π Your AI-Generated Blog")
|
94 |
+
st.write(blog_content)
|
requirements.txt
ADDED
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
pip
|
2 |
+
wheel
|
3 |
+
huggingface_hub
|
4 |
+
sentence-transformers
|
5 |
+
uvicorn
|
6 |
+
ctransformers
|
7 |
+
langchain
|
8 |
+
python-box
|
9 |
+
streamlit
|