Parul-23 commited on
Commit
84b5142
Β·
verified Β·
1 Parent(s): 790e38e

Upload 3 files

Browse files
Files changed (3) hide show
  1. README.md +1 -12
  2. app.py +133 -0
  3. requirements.txt +7 -0
README.md CHANGED
@@ -1,12 +1 @@
1
- ---
2
- title: Parul Codestrat Demo
3
- emoji: 🐠
4
- colorFrom: green
5
- colorTo: yellow
6
- sdk: gradio
7
- sdk_version: 5.43.1
8
- app_file: app.py
9
- pinned: false
10
- ---
11
-
12
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
1
+ title: CodeStratLabs Tagline Generator emoji: πŸš€ colorFrom: blue colorTo: green sdk: streamlit python_version: 3.10
 
 
 
 
 
 
 
 
 
 
 
app.py ADDED
@@ -0,0 +1,133 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import requests
3
+ from bs4 import BeautifulSoup
4
+ from transformers import pipeline
5
+ import torch
6
+
7
+ # --- App Configuration ---
8
+ st.set_page_config(
9
+ page_title="CodeStratLabs Tagline Generator",
10
+ page_icon="πŸš€",
11
+ layout="centered"
12
+ )
13
+
14
+ # --- Model Loading (with caching) ---
15
+ # Cache the model loading to prevent reloading on every interaction.
16
+ @st.cache_resource
17
+ def load_generator_model():
18
+ """Loads the text generation model from Hugging Face."""
19
+ # We use a smaller, faster model perfect for this kind of creative task.
20
+ # It's optimized for instruction-following.
21
+ return pipeline(
22
+ "text-generation",
23
+ model="HuggingFaceH4/zephyr-7b-beta",
24
+ torch_dtype=torch.bfloat16,
25
+ device_map="auto" # Automatically uses GPU if available
26
+ )
27
+
28
+ generator = load_generator_model()
29
+
30
+ # --- Web Scraping Function ---
31
+ @st.cache_data(ttl=3600) # Cache the scraped data for 1 hour
32
+ def get_codestrat_about_text():
33
+ """
34
+ Scrapes the 'About Us' text from the CodeStratLabs website.
35
+ Returns the text content or an error message.
36
+ """
37
+ url = "https://www.codestratlabs.com/#about"
38
+ try:
39
+ response = requests.get(url, headers={'User-Agent': 'Mozilla/5.0'})
40
+ response.raise_for_status() # Raise an exception for bad status codes (4xx or 5xx)
41
+
42
+ soup = BeautifulSoup(response.content, 'html.parser')
43
+
44
+ # Find the specific 'about' section by its ID
45
+ about_section = soup.find('section', id='about')
46
+
47
+ if about_section:
48
+ # Extract text from all paragraphs within the section for cleaner output
49
+ paragraphs = about_section.find_all('p')
50
+ full_text = ' '.join(p.get_text(strip=True) for p in paragraphs)
51
+ return full_text
52
+ else:
53
+ return "Error: Could not find the '#about' section on the page."
54
+
55
+ except requests.exceptions.RequestException as e:
56
+ return f"Error: Could not fetch the website content. Details: {e}"
57
+
58
+ # --- Main App UI ---
59
+
60
+ st.title("πŸš€ AI Tagline Generator for CodeStratLabs")
61
+ st.markdown(
62
+ "This app scrapes the live 'About Us' page from the CodeStratLabs website "
63
+ "and uses a Hugging Face LLM to generate creative marketing taglines."
64
+ )
65
+
66
+ # Fetch the text when the app loads
67
+ with st.spinner("Scraping the CodeStratLabs 'About Us' page..."):
68
+ about_text = get_codestrat_about_text()
69
+
70
+ if "Error:" in about_text:
71
+ st.error(about_text)
72
+ else:
73
+ st.success("Successfully scraped the website content!")
74
+ with st.expander("Click to see the scraped text"):
75
+ st.write(about_text)
76
+
77
+ st.markdown("---")
78
+
79
+ # --- Tagline Generation ---
80
+ st.header("Generate Marketing Taglines")
81
+
82
+ # User can customize the number of taglines
83
+ num_taglines = st.slider("Select number of taglines to generate:", min_value=3, max_value=10, value=5)
84
+
85
+ if st.button("✨ Generate Now"):
86
+ with st.spinner("πŸ€– The AI is thinking... Please wait."):
87
+ # We create a specific, instruction-based prompt for the model.
88
+ # This technique is called "prompt engineering".
89
+ prompt = f"""
90
+ <|system|>
91
+ You are a world-class marketing expert specializing in catchy, professional, and impactful taglines for technology companies. Your goal is to generate taglines based on the company description provided. The taglines should be short, memorable, and reflect the company's focus on AI, innovation, and business impact.</s>
92
+ <|user|>
93
+ Here is the company description for CodeStratLabs:
94
+ "{about_text}"
95
+
96
+ Please generate {num_taglines} unique marketing taglines based on this description.</s>
97
+ <|assistant|>
98
+ """
99
+
100
+ try:
101
+ # Generate the text using the pipeline
102
+ sequences = generator(
103
+ prompt,
104
+ max_new_tokens=150, # Max length of the generated response
105
+ do_sample=True,
106
+ temperature=0.7,
107
+ top_k=50,
108
+ top_p=0.95,
109
+ num_return_sequences=1,
110
+ )
111
+
112
+ # The model's output is a single string, so we need to parse it.
113
+ if sequences and sequences[0]['generated_text']:
114
+ # The response starts with the prompt, so we split it to get only the assistant's part.
115
+ assistant_response = sequences[0]['generated_text'].split("<|assistant|>")[1].strip()
116
+
117
+ # Clean up the output and split into a list
118
+ taglines = [line.strip() for line in assistant_response.split('\n') if line.strip()]
119
+
120
+ st.subheader("Here are your AI-generated taglines:")
121
+ for i, tagline in enumerate(taglines):
122
+ # Remove potential numbering like "1. " from the model's output
123
+ clean_tagline = tagline.split('. ', 1)[-1]
124
+ st.markdown(f"> {i+1}. **{clean_tagline}**")
125
+ else:
126
+ st.error("The model did not return a valid response. Please try again.")
127
+
128
+ except Exception as e:
129
+ st.error(f"An error occurred during AI generation: {e}")
130
+
131
+ st.markdown("---")
132
+ st.info("Built as a demonstration project for the Generative AI Engineer application at CodeStratLabs.")
133
+
requirements.txt ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ streamlit
2
+ requests
3
+ beautifulsoup4
4
+ transformers
5
+ torch
6
+ accelerate
7
+ bitsandbytes