lalitwale100 commited on
Commit
98963dd
·
verified ·
1 Parent(s): d1491d9

Model Changed

Browse files
Files changed (1) hide show
  1. app.py +83 -74
app.py CHANGED
@@ -1,74 +1,83 @@
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.")
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import google.generativeai as genai
3
+ from dotenv import load_dotenv
4
+ import os
5
+
6
+ load_dotenv()
7
+
8
+
9
+ st.set_page_config(
10
+ page_title="Blog Generation Using Google Gemini",
11
+ layout="centered",
12
+ initial_sidebar_state="collapsed"
13
+ )
14
+
15
+ # Configure the Google Gemini API
16
+ api_key = os.getenv("GOOGLE_API_KEY")
17
+ genai.configure(api_key=api_key)
18
+
19
+ # Function to list available models
20
+ def list_available_models():
21
+ try:
22
+ models = genai.list_models()
23
+ return [model.name for model in models if "generateContent" in model.supported_generation_methods]
24
+ except Exception as e:
25
+ st.error(f"Error listing models: {e}")
26
+ return []
27
+
28
+ # Generate blog content using Gemini
29
+ def generate_blog(keywords, no_words, blog_style):
30
+ try:
31
+ available_models = list_available_models()
32
+
33
+ if not available_models:
34
+ return "Unable to retrieve available models. Please check your API key and network connection."
35
+
36
+ # Select the first available generative model
37
+ model_name = available_models[6]
38
+ model = genai.GenerativeModel(model_name)
39
+
40
+ prompt = f"Generate a blog for {blog_style} job profile based on the keywords '{keywords}' within {no_words} words."
41
+
42
+ response = model.generate_content(prompt)
43
+ return response.text
44
+ except Exception as e:
45
+ return f"Error generating content: {e}"
46
+
47
+
48
+
49
+ st.header("Blog Generation App :earth_americas:")
50
+ st.write("This app uses Google's Gemini AI for generating blogs. Please provide your inputs below:")
51
+
52
+ # Input fields
53
+ keywords = st.text_input("Enter the Blog Keywords")
54
+ col1, col2 = st.columns([5, 5])
55
+ with col1:
56
+ no_words = st.text_input("Number of Words")
57
+ with col2:
58
+ blog_style = st.selectbox(
59
+ "Writing the blog for",
60
+ ["Researchers", "Engineers", "Doctors", "Content Creators", "Sportsman", "Businessman", "Common People"],
61
+ index=0
62
+ )
63
+
64
+ # Display available models
65
+ # if st.checkbox("Show available models"):
66
+ # models = list_available_models()
67
+ # if models:
68
+ # st.write("Available models:")
69
+ # for model in models:
70
+ # st.write(f"- {model}")
71
+ # else:
72
+ # st.write("No models available or unable to retrieve models.")
73
+
74
+ submit = st.button("Generate Blog")
75
+
76
+ # Generate response
77
+ if submit:
78
+ if keywords and no_words.isdigit() and int(no_words) > 0:
79
+ st.write("Generating blog...")
80
+ response = generate_blog(keywords, no_words, blog_style)
81
+ st.markdown(f"### Generated Blog:\n\n{response}")
82
+ else:
83
+ st.warning("Please provide valid inputs for all fields.")