prasannahf commited on
Commit
3141002
Β·
verified Β·
1 Parent(s): b852b87

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -13
app.py CHANGED
@@ -5,19 +5,13 @@ 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 Google Colab secrets
13
- GROQ_API_KEY = os.getenv("GROQ_API_KEY") # Get from Hugging Face secrets
14
  LANGSMITH_API_KEY = os.getenv("LANGSMITH_API_KEY")
15
 
16
- # βœ… Set environment variables
17
- os.environ["GROQ_API_KEY"] = GROQ_API_KEY
18
- os.environ["LANGCHAIN_TRACING_V2"] = "true"
19
- os.environ["LANGCHAIN_API_KEY"] = LANGSMITH_API_KEY
20
-
21
  # βœ… Initialize Groq LLM (for content generation)
22
  llm = ChatGroq(groq_api_key=GROQ_API_KEY, model_name="mixtral-8x7b-32768")
23
 
@@ -33,10 +27,9 @@ class State(TypedDict):
33
  language: str
34
 
35
  # βœ… Function to generate multiple blog titles using Groq
36
- @traceable(name="Generate Titles")
37
  def generate_titles(data):
38
  topic = data.get("topic", "")
39
- 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."
40
 
41
  response = llm([HumanMessage(content=prompt)])
42
  titles = response.content.strip().split("\n") # Get three titles as a list
@@ -44,7 +37,6 @@ def generate_titles(data):
44
  return {"titles": titles, "selected_title": titles[0]} # Default to first title
45
 
46
  # βœ… Function to generate blog content with tone using Groq
47
- @traceable(name="Generate Content")
48
  def generate_content(data):
49
  title = data.get("selected_title", "")
50
  tone = data.get("tone", "Neutral")
@@ -54,7 +46,6 @@ def generate_content(data):
54
  return {"content": response.content.strip()}
55
 
56
  # βœ… Function to generate summary using Groq
57
- @traceable(name="Generate Summary")
58
  def generate_summary(data):
59
  content = data.get("content", "")
60
  prompt = f"Summarize this blog post in a short and engaging way: {content}"
@@ -81,7 +72,6 @@ language_codes = {
81
  }
82
 
83
  # βœ… Function to translate blog content using NLLB-200
84
- @traceable(name="Translate Content")
85
  def translate_content(data):
86
  content = data.get("content", "")
87
  language = data.get("language", "English")
@@ -141,6 +131,21 @@ def make_blog_generation_graph():
141
 
142
  return graph_workflow.compile()
143
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
144
  # βœ… Gradio Interface with "Why Translate?" Section
145
  with gr.Blocks() as app:
146
  gr.Markdown(
 
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
  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
 
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
  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}"
 
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")
 
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:
138
+ return "⚠️ Please enter a topic.", "", "", "", ""
139
+
140
+ blog_agent = make_blog_generation_graph()
141
+ result = blog_agent.invoke({"topic": topic, "tone": tone, "language": language})
142
+
143
+ return result["titles"], result["selected_title"], result["content"], result["summary"], result["translated_content"]
144
+
145
+ except Exception as e:
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(