pratikshahp commited on
Commit
84cb511
·
verified ·
1 Parent(s): eb1a399

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -32
app.py CHANGED
@@ -1,42 +1,58 @@
1
- import gradio as gr
2
- from transformers import AutoTokenizer, AutoModelForCausalLM
3
- import torch
4
- from dotenv import load_dotenv
5
  import os
 
 
 
 
6
 
7
  # Load environment variables
8
  load_dotenv()
9
 
10
- # Load the model and tokenizer
11
- #tokenizer = AutoTokenizer.from_pretrained("google/gemma-2b")
12
- #model = AutoModelForCausalLM.from_pretrained("google/gemma-2b")
13
-
14
- tokenizer = AutoTokenizer.from_pretrained("tiiuae/falcon-rw-1b", trust_remote_code=True)
15
- model = AutoModelForCausalLM.from_pretrained("tiiuae/falcon-rw-1b", trust_remote_code=True)
16
-
17
- # Function to generate blog content
18
- def generate_blog(topic, keywords):
19
- prompt_template = f"""
20
- You are a content writer. Write a poem of a maximum of 10 sentences on the following topic.
21
- Topic: {topic}
22
- Poem:
23
- """
24
- input_ids = tokenizer(prompt_template, return_tensors="pt", max_length=512, truncation=True)
25
- outputs = model.generate(input_ids["input_ids"], max_length=800, num_return_sequences=1)
26
- blog_content = tokenizer.decode(outputs[0], skip_special_tokens=True)
27
-
28
- return blog_content
29
-
30
- # Gradio interface
31
- iface = gr.Interface(
32
- fn=generate_blog,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33
  inputs=[
34
- gr.Textbox(lines=2, placeholder="Enter the poem topic", label="Blog Topic"),
 
35
  ],
36
- outputs=gr.Textbox(label="Generated Poem"),
37
- title="Poem Generator",
38
- description="Generate a poem based on the providing a topic."
 
39
  )
40
 
41
  if __name__ == "__main__":
42
- iface.launch(share=True) # Set share=True to generate a public link
 
 
 
 
 
1
  import os
2
+ from dotenv import load_dotenv
3
+ import gradio as gr
4
+ from langchain import PromptTemplate
5
+ from langchain_huggingface import HuggingFaceEndpoint
6
 
7
  # Load environment variables
8
  load_dotenv()
9
 
10
+ HF_TOKEN = os.getenv("HF_TOKEN")
11
+ # Initialize the HuggingFace model
12
+ llm = HuggingFaceEndpoint(
13
+ repo_id="mistralai/Mistral-7B-Instruct-v0.3",
14
+ huggingfacehub_api_token=HF_TOKEN,
15
+ temperature=0.7,
16
+ max_new_tokens=200
17
+ )
18
+ # Define a prompt template for generating a blog
19
+ TEMPLATE = """
20
+ Write a detailed blog post on the following topic:
21
+ Topic: {topic}
22
+ Make sure the blog post is informative, engaging, and well-structured.
23
+ """
24
+
25
+ # Create a prompt template instance
26
+ blog_prompt_template = PromptTemplate(input_variables=["topic"], template=TEMPLATE)
27
+ prompt = blog_prompt_template
28
+ # Initialize the LLMChain
29
+ blog_chain = llm | prompt
30
+
31
+ def generate_blog_post(topic: str, author_name: str) -> str:
32
+ if topic:
33
+ # Generate the blog post
34
+ blog_post = blog_chain.run({"topic": topic})
35
+
36
+ # Add author name if provided
37
+ if author_name:
38
+ blog_post = f"**By {author_name}**\n\n" + blog_post
39
+
40
+ return blog_post
41
+ else:
42
+ return "Please enter a topic for the blog post."
43
+
44
+ # Define the Gradio interface
45
+ interface = gr.Interface(
46
+ fn=generate_blog_post,
47
  inputs=[
48
+ gr.Textbox(label="Blog Topic", placeholder="Enter the topic here"),
49
+ gr.Textbox(label="Author Name", placeholder="Optional", optional=True)
50
  ],
51
+ outputs="text",
52
+ title="AI Blog Generator",
53
+ description="Welcome to the AI Blog Generator. This tool allows you to generate high-quality, engaging blog posts in just a few clicks. Simply provide a topic, and the AI will create a detailed blog post for you.",
54
+ theme="default"
55
  )
56
 
57
  if __name__ == "__main__":
58
+ interface.launch()