Spaces:
Running
Running
Upload 3 files
Browse files- app3.py +74 -0
- requirements.txt +8 -0
- style.css +4 -0
app3.py
ADDED
@@ -0,0 +1,74 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from langchain.prompts import PromptTemplate
|
3 |
+
from langchain.chains import LLMChain
|
4 |
+
from langchain_community.llms import HuggingFaceHub
|
5 |
+
from huggingface_hub import login
|
6 |
+
from dotenv import load_dotenv
|
7 |
+
import os
|
8 |
+
|
9 |
+
load_dotenv()
|
10 |
+
|
11 |
+
# Authenticate with Hugging Face Hub
|
12 |
+
hf_token = os.getenv("HUGGINGFACEHUB_API_TOKEN")
|
13 |
+
login(hf_token)
|
14 |
+
|
15 |
+
# Initialize LLM using HuggingFaceHub
|
16 |
+
llm = HuggingFaceHub(
|
17 |
+
repo_id="caffsean/t5-base-finetuned-keyword-to-text-generation",
|
18 |
+
model_kwargs={
|
19 |
+
"temperature": 0.7, # Adjust for creativity
|
20 |
+
"max_length": 512, # Adjust for desired output length
|
21 |
+
}
|
22 |
+
)
|
23 |
+
|
24 |
+
# Define a function to generate responses
|
25 |
+
def get_blog_response(keywords: str, no_words: str, blog_style: str) -> str:
|
26 |
+
# Define a prompt template
|
27 |
+
template = "Generate a blog for {blog_style} job profile based on the keywords '{keywords}' within {no_words} words."
|
28 |
+
prompt = PromptTemplate(
|
29 |
+
input_variables=["keywords", "no_words", "blog_style"],
|
30 |
+
template=template
|
31 |
+
)
|
32 |
+
|
33 |
+
# Use LangChain LLMChain for structured interaction
|
34 |
+
chain = LLMChain(llm=llm, prompt=prompt)
|
35 |
+
response = chain.run(keywords=keywords, no_words=no_words, blog_style=blog_style)
|
36 |
+
return response
|
37 |
+
|
38 |
+
# Streamlit UI
|
39 |
+
st.set_page_config(
|
40 |
+
page_title="Blog Generation Using LangChain and Hugging Face",
|
41 |
+
layout="centered",
|
42 |
+
initial_sidebar_state="collapsed"
|
43 |
+
)
|
44 |
+
|
45 |
+
st.header("Blog Generation App :earth_americas:")
|
46 |
+
|
47 |
+
st.write("This app uses LangChain and a fine-tuned T5 model for generating blogs. Please provide your inputs below:")
|
48 |
+
|
49 |
+
# Input fields
|
50 |
+
keywords = st.text_input("Enter the Blog Keywords")
|
51 |
+
|
52 |
+
col1, col2 = st.columns([5, 5])
|
53 |
+
with col1:
|
54 |
+
no_words = st.text_input("Number of Words")
|
55 |
+
with col2:
|
56 |
+
blog_style = st.selectbox(
|
57 |
+
"Writing the blog for",
|
58 |
+
["Researchers", "Engineers", "Doctors", "Content Creators", "Sportsman", "Businessman", "Common People"],
|
59 |
+
index=0
|
60 |
+
)
|
61 |
+
|
62 |
+
submit = st.button("Generate Blog")
|
63 |
+
|
64 |
+
# Generate response
|
65 |
+
if submit:
|
66 |
+
if keywords and no_words.isdigit() and int(no_words) > 0:
|
67 |
+
try:
|
68 |
+
st.write("Generating blog...")
|
69 |
+
response = get_blog_response(keywords, no_words, blog_style)
|
70 |
+
st.markdown(f"### Generated Blog:\n\n{response}")
|
71 |
+
except Exception as e:
|
72 |
+
st.error(f"An error occurred: {e}")
|
73 |
+
else:
|
74 |
+
st.warning("Please provide valid inputs for all fields.")
|
requirements.txt
ADDED
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
langchain
|
2 |
+
streamlit
|
3 |
+
ctransformers
|
4 |
+
sentence-transformers
|
5 |
+
uvicorn
|
6 |
+
langchain_community
|
7 |
+
transformers
|
8 |
+
python-dotenv
|
style.css
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
* {
|
2 |
+
background-color: #edb88b;
|
3 |
+
padding: 0;
|
4 |
+
}
|