Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,15 +5,17 @@ import torch
|
|
| 5 |
from langgraph.graph import StateGraph, START, END
|
| 6 |
from langchain.schema import HumanMessage
|
| 7 |
from langchain_groq import ChatGroq
|
| 8 |
-
from langsmith import traceable
|
| 9 |
from typing import TypedDict
|
| 10 |
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
|
|
|
|
| 11 |
|
| 12 |
-
# β
Load API keys from
|
| 13 |
-
GROQ_API_KEY =
|
| 14 |
-
LANGSMITH_API_KEY =
|
| 15 |
|
| 16 |
-
# β
Set
|
|
|
|
| 17 |
os.environ["LANGCHAIN_TRACING_V2"] = "true"
|
| 18 |
os.environ["LANGCHAIN_API_KEY"] = LANGSMITH_API_KEY
|
| 19 |
|
|
@@ -32,18 +34,18 @@ class State(TypedDict):
|
|
| 32 |
language: str
|
| 33 |
|
| 34 |
# β
Function to generate multiple blog titles using Groq
|
| 35 |
-
@traceable(name="Generate Titles")
|
| 36 |
def generate_titles(data):
|
| 37 |
topic = data.get("topic", "")
|
| 38 |
-
prompt = f"Generate three short and catchy blog titles for the topic: {topic}. Each title should be under 10 words. Separate them with new lines."
|
| 39 |
|
| 40 |
response = llm([HumanMessage(content=prompt)])
|
| 41 |
-
titles = response.content.strip().split("\n")
|
| 42 |
|
| 43 |
-
return {"titles": titles, "selected_title": titles[0]}
|
| 44 |
|
| 45 |
# β
Function to generate blog content with tone using Groq
|
| 46 |
-
@traceable(name="Generate Content")
|
| 47 |
def generate_content(data):
|
| 48 |
title = data.get("selected_title", "")
|
| 49 |
tone = data.get("tone", "Neutral")
|
|
@@ -53,7 +55,7 @@ def generate_content(data):
|
|
| 53 |
return {"content": response.content.strip()}
|
| 54 |
|
| 55 |
# β
Function to generate summary using Groq
|
| 56 |
-
@traceable(name="Generate Summary")
|
| 57 |
def generate_summary(data):
|
| 58 |
content = data.get("content", "")
|
| 59 |
prompt = f"Summarize this blog post in a short and engaging way: {content}"
|
|
@@ -63,7 +65,7 @@ def generate_summary(data):
|
|
| 63 |
|
| 64 |
# β
Load translation model (NLLB-200)
|
| 65 |
def load_translation_model():
|
| 66 |
-
model_name = "facebook/nllb-200-distilled-
|
| 67 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 68 |
model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
|
| 69 |
return tokenizer, model
|
|
@@ -80,19 +82,19 @@ language_codes = {
|
|
| 80 |
}
|
| 81 |
|
| 82 |
# β
Function to translate blog content using NLLB-200
|
| 83 |
-
@traceable(name="Translate Content")
|
| 84 |
def translate_content(data):
|
| 85 |
content = data.get("content", "")
|
| 86 |
language = data.get("language", "English")
|
| 87 |
|
| 88 |
if language == "English":
|
| 89 |
-
return {"translated_content": content}
|
| 90 |
|
| 91 |
-
tgt_lang = language_codes.get(language, "eng_Latn")
|
| 92 |
|
| 93 |
# β
Split content into smaller chunks (Avoids token limit issues)
|
| 94 |
-
max_length = 512
|
| 95 |
-
sentences = content.split(". ")
|
| 96 |
chunks = []
|
| 97 |
current_chunk = ""
|
| 98 |
|
|
@@ -114,6 +116,7 @@ def translate_content(data):
|
|
| 114 |
translated_text = tokenizer.decode(translated_tokens[0], skip_special_tokens=True)
|
| 115 |
translated_chunks.append(translated_text.strip())
|
| 116 |
|
|
|
|
| 117 |
full_translation = " ".join(translated_chunks)
|
| 118 |
|
| 119 |
return {"translated_content": full_translation}
|
|
@@ -127,42 +130,28 @@ def make_blog_generation_graph():
|
|
| 127 |
graph_workflow.add_node("title_generation", generate_titles)
|
| 128 |
graph_workflow.add_node("content_generation", generate_content)
|
| 129 |
graph_workflow.add_node("summary_generation", generate_summary)
|
| 130 |
-
graph_workflow.add_node("translation", translate_content)
|
| 131 |
|
| 132 |
# Define Execution Order
|
| 133 |
graph_workflow.add_edge(START, "title_generation")
|
| 134 |
graph_workflow.add_edge("title_generation", "content_generation")
|
| 135 |
-
graph_workflow.add_edge("content_generation", "summary_generation")
|
| 136 |
-
graph_workflow.add_edge("content_generation", "translation")
|
| 137 |
graph_workflow.add_edge("summary_generation", END)
|
| 138 |
graph_workflow.add_edge("translation", END)
|
| 139 |
|
| 140 |
return graph_workflow.compile()
|
| 141 |
|
| 142 |
-
# β
|
| 143 |
-
def generate_blog(topic, tone, language):
|
| 144 |
-
try:
|
| 145 |
-
if not topic:
|
| 146 |
-
return "β οΈ Please enter a topic.", "", "", "", ""
|
| 147 |
-
|
| 148 |
-
blog_agent = make_blog_generation_graph()
|
| 149 |
-
result = blog_agent.invoke({"topic": topic, "tone": tone, "language": language})
|
| 150 |
-
|
| 151 |
-
return result["titles"], result["selected_title"], result["content"], result["summary"], result["translated_content"]
|
| 152 |
-
|
| 153 |
-
except Exception as e:
|
| 154 |
-
error_message = f"β οΈ Error: {str(e)}\n{traceback.format_exc()}"
|
| 155 |
-
return error_message, "", "", "", ""
|
| 156 |
-
|
| 157 |
-
# β
Gradio UI
|
| 158 |
with gr.Blocks() as app:
|
| 159 |
gr.Markdown(
|
| 160 |
"""
|
| 161 |
### π Why Translate?
|
| 162 |
-
|
| 163 |
-
-
|
| 164 |
-
-
|
| 165 |
-
-
|
|
|
|
| 166 |
"""
|
| 167 |
)
|
| 168 |
|
|
@@ -174,13 +163,14 @@ with gr.Blocks() as app:
|
|
| 174 |
gr.Dropdown(["English", "Hindi", "Telugu", "Spanish", "French"], label="Translate Blog To", value="English"),
|
| 175 |
],
|
| 176 |
outputs=[
|
| 177 |
-
gr.Textbox(label="Suggested Blog Titles"),
|
| 178 |
gr.Textbox(label="Selected Blog Title"),
|
| 179 |
gr.Textbox(label="Generated Blog Content"),
|
| 180 |
gr.Textbox(label="Blog Summary"),
|
| 181 |
gr.Textbox(label="Translated Blog Content"),
|
| 182 |
],
|
| 183 |
-
title="π AI-Powered Blog Generator",
|
|
|
|
| 184 |
)
|
| 185 |
|
| 186 |
# β
Launch the Gradio App
|
|
|
|
| 5 |
from langgraph.graph import StateGraph, START, END
|
| 6 |
from langchain.schema import HumanMessage
|
| 7 |
from langchain_groq import ChatGroq
|
| 8 |
+
from langsmith import traceable
|
| 9 |
from typing import TypedDict
|
| 10 |
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
|
| 11 |
+
from google.colab import userdata # Only needed in Google Colab
|
| 12 |
|
| 13 |
+
# β
Load API keys from Google Colab secrets
|
| 14 |
+
GROQ_API_KEY = userdata.get('GROQ_API_KEY') # Ensure this is set in Colab
|
| 15 |
+
LANGSMITH_API_KEY = userdata.get('LANGSMITH_API_KEY')
|
| 16 |
|
| 17 |
+
# β
Set environment variables
|
| 18 |
+
os.environ["GROQ_API_KEY"] = GROQ_API_KEY
|
| 19 |
os.environ["LANGCHAIN_TRACING_V2"] = "true"
|
| 20 |
os.environ["LANGCHAIN_API_KEY"] = LANGSMITH_API_KEY
|
| 21 |
|
|
|
|
| 34 |
language: str
|
| 35 |
|
| 36 |
# β
Function to generate multiple blog titles using Groq
|
| 37 |
+
@traceable(name="Generate Titles")
|
| 38 |
def generate_titles(data):
|
| 39 |
topic = data.get("topic", "")
|
| 40 |
+
prompt = f"Generate **three short and catchy blog titles** for the topic: {topic}. Each title should be under 10 words. Separate them with new lines."
|
| 41 |
|
| 42 |
response = llm([HumanMessage(content=prompt)])
|
| 43 |
+
titles = response.content.strip().split("\n") # Get three titles as a list
|
| 44 |
|
| 45 |
+
return {"titles": titles, "selected_title": titles[0]} # Default to first title
|
| 46 |
|
| 47 |
# β
Function to generate blog content with tone using Groq
|
| 48 |
+
@traceable(name="Generate Content")
|
| 49 |
def generate_content(data):
|
| 50 |
title = data.get("selected_title", "")
|
| 51 |
tone = data.get("tone", "Neutral")
|
|
|
|
| 55 |
return {"content": response.content.strip()}
|
| 56 |
|
| 57 |
# β
Function to generate summary using Groq
|
| 58 |
+
@traceable(name="Generate Summary")
|
| 59 |
def generate_summary(data):
|
| 60 |
content = data.get("content", "")
|
| 61 |
prompt = f"Summarize this blog post in a short and engaging way: {content}"
|
|
|
|
| 65 |
|
| 66 |
# β
Load translation model (NLLB-200)
|
| 67 |
def load_translation_model():
|
| 68 |
+
model_name = "facebook/nllb-200-distilled-600M" # Efficient model for 200+ languages
|
| 69 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 70 |
model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
|
| 71 |
return tokenizer, model
|
|
|
|
| 82 |
}
|
| 83 |
|
| 84 |
# β
Function to translate blog content using NLLB-200
|
| 85 |
+
@traceable(name="Translate Content")
|
| 86 |
def translate_content(data):
|
| 87 |
content = data.get("content", "")
|
| 88 |
language = data.get("language", "English")
|
| 89 |
|
| 90 |
if language == "English":
|
| 91 |
+
return {"translated_content": content} # No translation needed
|
| 92 |
|
| 93 |
+
tgt_lang = language_codes.get(language, "eng_Latn") # Default to English if not found
|
| 94 |
|
| 95 |
# β
Split content into smaller chunks (Avoids token limit issues)
|
| 96 |
+
max_length = 512 # Adjust based on model limitations
|
| 97 |
+
sentences = content.split(". ") # Split at sentence level
|
| 98 |
chunks = []
|
| 99 |
current_chunk = ""
|
| 100 |
|
|
|
|
| 116 |
translated_text = tokenizer.decode(translated_tokens[0], skip_special_tokens=True)
|
| 117 |
translated_chunks.append(translated_text.strip())
|
| 118 |
|
| 119 |
+
# β
Combine all translated chunks into final text
|
| 120 |
full_translation = " ".join(translated_chunks)
|
| 121 |
|
| 122 |
return {"translated_content": full_translation}
|
|
|
|
| 130 |
graph_workflow.add_node("title_generation", generate_titles)
|
| 131 |
graph_workflow.add_node("content_generation", generate_content)
|
| 132 |
graph_workflow.add_node("summary_generation", generate_summary)
|
| 133 |
+
graph_workflow.add_node("translation", translate_content) # Ensures only blog content is translated
|
| 134 |
|
| 135 |
# Define Execution Order
|
| 136 |
graph_workflow.add_edge(START, "title_generation")
|
| 137 |
graph_workflow.add_edge("title_generation", "content_generation")
|
| 138 |
+
graph_workflow.add_edge("content_generation", "summary_generation") # Summary only generated from content
|
| 139 |
+
graph_workflow.add_edge("content_generation", "translation") # Translation happens for content only
|
| 140 |
graph_workflow.add_edge("summary_generation", END)
|
| 141 |
graph_workflow.add_edge("translation", END)
|
| 142 |
|
| 143 |
return graph_workflow.compile()
|
| 144 |
|
| 145 |
+
# β
Gradio Interface with "Why Translate?" Section
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 146 |
with gr.Blocks() as app:
|
| 147 |
gr.Markdown(
|
| 148 |
"""
|
| 149 |
### π Why Translate?
|
| 150 |
+
We provide translation to make the blog content **accessible to a global audience**.
|
| 151 |
+
- π£οΈ **Multilingual Support** β Read blogs in your preferred language.
|
| 152 |
+
- π **Expand Reach** β Reach international readers.
|
| 153 |
+
- β
**Better Understanding** β Enjoy content in a language you're comfortable with.
|
| 154 |
+
- π€ **AI-Powered Accuracy** β Uses advanced AI models for precise translation.
|
| 155 |
"""
|
| 156 |
)
|
| 157 |
|
|
|
|
| 163 |
gr.Dropdown(["English", "Hindi", "Telugu", "Spanish", "French"], label="Translate Blog To", value="English"),
|
| 164 |
],
|
| 165 |
outputs=[
|
| 166 |
+
gr.Textbox(label="Suggested Blog Titles (Choose One)"), # Displays multiple title suggestions
|
| 167 |
gr.Textbox(label="Selected Blog Title"),
|
| 168 |
gr.Textbox(label="Generated Blog Content"),
|
| 169 |
gr.Textbox(label="Blog Summary"),
|
| 170 |
gr.Textbox(label="Translated Blog Content"),
|
| 171 |
],
|
| 172 |
+
title="π AI-Powered Blog Generator with Multi-Title Suggestions",
|
| 173 |
+
description="Generate high-quality blogs using Groq AI, customize tone, translate using NLLB-200, and get interactive summaries. Select from multiple title suggestions!",
|
| 174 |
)
|
| 175 |
|
| 176 |
# β
Launch the Gradio App
|