Spaces:
Runtime error
Runtime error
Update app.py
Browse files
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 |
-
|
11 |
-
#
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
inputs=[
|
34 |
-
gr.Textbox(
|
|
|
35 |
],
|
36 |
-
outputs=
|
37 |
-
title="
|
38 |
-
description="
|
|
|
39 |
)
|
40 |
|
41 |
if __name__ == "__main__":
|
42 |
-
|
|
|
|
|
|
|
|
|
|
|
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()
|