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

Update app.py

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