akhud commited on
Commit
ae74582
1 Parent(s): 9c6f15b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -47
app.py CHANGED
@@ -1,61 +1,54 @@
 
 
1
  import streamlit as st
 
 
 
2
  from langchain.prompts import PromptTemplate
3
- from langchain.llms import CTransformers
4
- # from transformers import AutoModel
5
-
6
- ## Function To get response from LLAma 2 model
7
-
8
- def getLLamaresponse(input_text,no_words,blog_style):
9
-
10
- # Load model directly
11
-
12
- ### LLama2 model
13
- llm=CTransformers(model='models/llama-2-7b-chat.ggmlv3.q8_0.bin',
14
- model_type='llama',
15
- config={'max_new_tokens':256,
16
- 'temperature':0.01})
17
-
18
- ## Prompt Template
19
-
20
- template="""
21
- Write a blog for {blog_style} profile for a topic {input_text}
22
- within {no_words} words.
23
- """
24
-
25
- prompt=PromptTemplate(input_variables=["blog_style","input_text",'no_words'],
26
- template=template)
27
-
28
- ## Generate the ressponse from the LLama 2 model
29
- response=llm(prompt.format(blog_style=blog_style,input_text=input_text,no_words=no_words))
30
- print(response)
31
- return response
32
-
33
 
 
 
34
 
 
 
 
 
 
 
35
 
 
 
36
 
 
 
 
 
37
 
38
- st.set_page_config(page_title="Generate Blogs",
39
- page_icon='♜',
40
- layout='centered',
41
- initial_sidebar_state='collapsed')
42
 
43
- st.header("Lets Generate Blogs Together ♜")
44
 
45
- input_text=st.text_input("Enter the Blog Topic")
 
46
 
47
- ## creating to more columns for additonal 2 fields
 
48
 
49
- col1,col2=st.columns([5,5])
 
50
 
 
51
  with col1:
52
- no_words=st.text_input('No of Words')
 
 
53
  with col2:
54
- blog_style=st.selectbox('Writing the blog for',
55
- ('Kids - 5 Year olds','Grad Students','Researchers','Data Scientist','Common People',),index=0)
56
-
57
- submit=st.button("Generate")
58
-
59
- ## Final response
60
- if submit:
61
- st.write(getLLamaresponse(input_text,no_words,blog_style))
 
 
 
1
+ from dotenv import load_dotenv
2
+ load_dotenv()
3
  import streamlit as st
4
+ import os
5
+ import google.generativeai as genai
6
+ from langchain_google_genai import ChatGoogleGenerativeAI
7
  from langchain.prompts import PromptTemplate
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
 
9
+ genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
10
+ model = ChatGoogleGenerativeAI(model="gemini-pro")
11
 
12
+ def gemini_model(input_text,no_of_words,blog_style):
13
+ # here we are creating a template for the prompt
14
+ template = """
15
+ Write a blog on the topic of {input_text} for {blog_style} audience.
16
+ The blog should be {no_of_words} words long.
17
+ """
18
 
19
+ # here we are creating a prompt using the template and the input variables
20
+ prompt = PromptTemplate(input_variables=["input_text","blog_style","no_of_words"],template=template)
21
 
22
+ # here we are generating the blog
23
+ response = model.invoke(prompt.format(input_text=input_text,blog_style=blog_style,no_of_words=no_of_words))
24
+ print(response)
25
+ return response.content
26
 
 
 
 
 
27
 
28
+ st.set_page_config(page_title="Blog Generator", initial_sidebar_state="collapsed", layout="centered")
29
 
30
+ # Header
31
+ st.title("📝 Generate Blog")
32
 
33
+ # Input Section
34
+ input_text = st.text_input("🔍 Enter the topic of the blog you want to generate")
35
 
36
+ # Creating 2 columns for additional 2 fields
37
+ col1, col2 = st.columns([2, 2])
38
 
39
+ # Number of words input
40
  with col1:
41
+ no_of_words = st.text_input("📏 Number of Words", value="500")
42
+
43
+ # Blog style selection
44
  with col2:
45
+ blog_style = st.selectbox("📝 Writing the blog for", ("Researchers or Professionals", "General Audience"), index=0)
46
+
47
+ # Generate Button
48
+ submit_button = st.button("Generate Blog 🚀")
49
+
50
+ # Display the generated blog on button click
51
+ if submit_button:
52
+ st.success("📖 **Generated Blog:**")
53
+ generated_blog = gemini_model(input_text, no_of_words, blog_style)
54
+ st.write(generated_blog)