prasannahf commited on
Commit
4b0ede1
Β·
verified Β·
1 Parent(s): baa477e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -22
app.py CHANGED
@@ -5,13 +5,18 @@ import torch
5
  from langgraph.graph import StateGraph, START, END
6
  from langchain.schema import HumanMessage
7
  from langchain_groq import ChatGroq
 
8
  from typing import TypedDict
9
  from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
10
 
11
  # βœ… Load API keys from Hugging Face Secrets
12
- GROQ_API_KEY = os.getenv("GROQ_API_KEY") # Hugging Face Environment Variable
13
  LANGSMITH_API_KEY = os.getenv("LANGSMITH_API_KEY")
14
 
 
 
 
 
15
  # βœ… Initialize Groq LLM (for content generation)
16
  llm = ChatGroq(groq_api_key=GROQ_API_KEY, model_name="mixtral-8x7b-32768")
17
 
@@ -27,16 +32,18 @@ class State(TypedDict):
27
  language: str
28
 
29
  # βœ… Function to generate multiple blog titles using Groq
 
30
  def generate_titles(data):
31
  topic = data.get("topic", "")
32
  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."
33
 
34
  response = llm([HumanMessage(content=prompt)])
35
- titles = response.content.strip().split("\n") # Get three titles as a list
36
 
37
- return {"titles": titles, "selected_title": titles[0]} # Default to first title
38
 
39
  # βœ… Function to generate blog content with tone using Groq
 
40
  def generate_content(data):
41
  title = data.get("selected_title", "")
42
  tone = data.get("tone", "Neutral")
@@ -46,6 +53,7 @@ def generate_content(data):
46
  return {"content": response.content.strip()}
47
 
48
  # βœ… Function to generate summary using Groq
 
49
  def generate_summary(data):
50
  content = data.get("content", "")
51
  prompt = f"Summarize this blog post in a short and engaging way: {content}"
@@ -55,7 +63,7 @@ def generate_summary(data):
55
 
56
  # βœ… Load translation model (NLLB-200)
57
  def load_translation_model():
58
- model_name = "facebook/nllb-200-distilled-600M" # Efficient model for 200+ languages
59
  tokenizer = AutoTokenizer.from_pretrained(model_name)
60
  model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
61
  return tokenizer, model
@@ -72,18 +80,19 @@ language_codes = {
72
  }
73
 
74
  # βœ… Function to translate blog content using NLLB-200
 
75
  def translate_content(data):
76
  content = data.get("content", "")
77
  language = data.get("language", "English")
78
 
79
  if language == "English":
80
- return {"translated_content": content} # No translation needed
81
 
82
- tgt_lang = language_codes.get(language, "eng_Latn") # Default to English if not found
83
 
84
  # βœ… Split content into smaller chunks (Avoids token limit issues)
85
- max_length = 512 # Adjust based on model limitations
86
- sentences = content.split(". ") # Split at sentence level
87
  chunks = []
88
  current_chunk = ""
89
 
@@ -105,7 +114,6 @@ def translate_content(data):
105
  translated_text = tokenizer.decode(translated_tokens[0], skip_special_tokens=True)
106
  translated_chunks.append(translated_text.strip())
107
 
108
- # βœ… Combine all translated chunks into final text
109
  full_translation = " ".join(translated_chunks)
110
 
111
  return {"translated_content": full_translation}
@@ -119,19 +127,19 @@ def make_blog_generation_graph():
119
  graph_workflow.add_node("title_generation", generate_titles)
120
  graph_workflow.add_node("content_generation", generate_content)
121
  graph_workflow.add_node("summary_generation", generate_summary)
122
- graph_workflow.add_node("translation", translate_content) # Ensures only blog content is translated
123
 
124
  # Define Execution Order
125
  graph_workflow.add_edge(START, "title_generation")
126
  graph_workflow.add_edge("title_generation", "content_generation")
127
- graph_workflow.add_edge("content_generation", "summary_generation") # Summary only generated from content
128
- graph_workflow.add_edge("content_generation", "translation") # Translation happens for content only
129
  graph_workflow.add_edge("summary_generation", END)
130
  graph_workflow.add_edge("translation", END)
131
 
132
  return graph_workflow.compile()
133
 
134
- # βœ… Function to generate blog content (Missing function added)
135
  def generate_blog(topic, tone, language):
136
  try:
137
  if not topic:
@@ -146,16 +154,15 @@ def generate_blog(topic, tone, language):
146
  error_message = f"⚠️ Error: {str(e)}\n{traceback.format_exc()}"
147
  return error_message, "", "", "", ""
148
 
149
- # βœ… Gradio Interface with "Why Translate?" Section
150
  with gr.Blocks() as app:
151
  gr.Markdown(
152
  """
153
  ### 🌍 Why Translate?
154
- We provide translation to make the blog content **accessible to a global audience**.
155
- - πŸ—£οΈ **Multilingual Support** – Read blogs in your preferred language.
156
- - 🌎 **Expand Reach** – Reach international readers.
157
- - βœ… **Better Understanding** – Enjoy content in a language you're comfortable with.
158
- - πŸ€– **AI-Powered Accuracy** – Uses advanced AI models for precise translation.
159
  """
160
  )
161
 
@@ -167,14 +174,13 @@ with gr.Blocks() as app:
167
  gr.Dropdown(["English", "Hindi", "Telugu", "Spanish", "French"], label="Translate Blog To", value="English"),
168
  ],
169
  outputs=[
170
- gr.Textbox(label="Suggested Blog Titles (Choose One)"), # Displays multiple title suggestions
171
  gr.Textbox(label="Selected Blog Title"),
172
  gr.Textbox(label="Generated Blog Content"),
173
  gr.Textbox(label="Blog Summary"),
174
  gr.Textbox(label="Translated Blog Content"),
175
  ],
176
- title="πŸš€ AI-Powered Blog Generator with Multi-Title Suggestions",
177
- description="Generate high-quality blogs using Groq AI, customize tone, translate using NLLB-200, and get interactive summaries. Select from multiple title suggestions!",
178
  )
179
 
180
  # βœ… 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 # βœ… Added LangSmith for Debugging
9
  from typing import TypedDict
10
  from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
11
 
12
  # βœ… Load API keys from Hugging Face Secrets
13
+ GROQ_API_KEY = os.getenv("GROQ_API_KEY")
14
  LANGSMITH_API_KEY = os.getenv("LANGSMITH_API_KEY")
15
 
16
+ # βœ… Set LangSmith Debugging
17
+ os.environ["LANGCHAIN_TRACING_V2"] = "true"
18
+ os.environ["LANGCHAIN_API_KEY"] = LANGSMITH_API_KEY
19
+
20
  # βœ… Initialize Groq LLM (for content generation)
21
  llm = ChatGroq(groq_api_key=GROQ_API_KEY, model_name="mixtral-8x7b-32768")
22
 
 
32
  language: str
33
 
34
  # βœ… Function to generate multiple blog titles using Groq
35
+ @traceable(name="Generate Titles") # βœ… Debugging with LangSmith
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") # βœ… Debugging with LangSmith
47
  def generate_content(data):
48
  title = data.get("selected_title", "")
49
  tone = data.get("tone", "Neutral")
 
53
  return {"content": response.content.strip()}
54
 
55
  # βœ… Function to generate summary using Groq
56
+ @traceable(name="Generate Summary") # βœ… Debugging with LangSmith
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
 
64
  # βœ… Load translation model (NLLB-200)
65
  def load_translation_model():
66
+ model_name = "facebook/nllb-200-distilled-600M"
67
  tokenizer = AutoTokenizer.from_pretrained(model_name)
68
  model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
69
  return tokenizer, model
 
80
  }
81
 
82
  # βœ… Function to translate blog content using NLLB-200
83
+ @traceable(name="Translate Content") # βœ… Debugging with LangSmith
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
  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
  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
+ # βœ… Function to generate blog content (Fixed)
143
  def generate_blog(topic, tone, language):
144
  try:
145
  if not topic:
 
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
+ - πŸ—£οΈ **Multilingual Support**
163
+ - 🌎 **Expand Reach**
164
+ - βœ… **Better Understanding**
165
+ - πŸ€– **AI-Powered Accuracy**
 
166
  """
167
  )
168
 
 
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