Ashar11 commited on
Commit
e92364d
·
verified ·
1 Parent(s): 0c512fa

Update service.py

Browse files
Files changed (1) hide show
  1. service.py +100 -2
service.py CHANGED
@@ -3,7 +3,8 @@ from pydantic import BaseModel
3
  import os
4
  from langgraph.graph import END
5
  from langchain_google_genai import ChatGoogleGenerativeAI
6
- from langchain_community.tools import DuckDuckGoSearchRun
 
7
  from langgraph.graph import StateGraph
8
  from typing import Dict, List
9
  from docx import Document
@@ -12,10 +13,12 @@ import shutil
12
  from streamlit.runtime.uploaded_file_manager import UploadedFile
13
  # ✅ Set Google Gemini API Key
14
  os.environ["GOOGLE_API_KEY"] = "AIzaSyCxfUhnGGmVjWy1PZJIpjv3hfBDkSDcWJA"
 
15
  # ✅ Initialize Google Gemini Model
16
  llm = ChatGoogleGenerativeAI(model="gemini-2.0-flash")
17
  # ✅ Define Search Tool
18
- search_tool = DuckDuckGoSearchRun(region="uk-en", safe="strict")
 
19
  app = FastAPI()
20
  class ContentState(Dict):
21
  idea: str
@@ -66,6 +69,7 @@ def research_task(state: ContentState) -> ContentState:
66
  research_query = f"{state['idea']}-{state['company_name']} - {state['services']}"
67
  research_data = search_tool.run(research_query )
68
  state.update({"research_data": research_data})
 
69
  return state
70
 
71
  # ✅ SEO Optimization Task
@@ -1146,6 +1150,100 @@ You are a **senior UK-based content strategist and SEO specialist**. Your job is
1146
  })
1147
 
1148
  return state
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1149
  def upload_file(state: ContentState) -> ContentState:
1150
  uploaded_file = state["file_path"] # Extract uploaded file
1151
 
 
3
  import os
4
  from langgraph.graph import END
5
  from langchain_google_genai import ChatGoogleGenerativeAI
6
+ # from langchain_community.tools import DuckDuckGoSearchRun
7
+ from langchain_tavily import TavilySearch
8
  from langgraph.graph import StateGraph
9
  from typing import Dict, List
10
  from docx import Document
 
13
  from streamlit.runtime.uploaded_file_manager import UploadedFile
14
  # ✅ Set Google Gemini API Key
15
  os.environ["GOOGLE_API_KEY"] = "AIzaSyCxfUhnGGmVjWy1PZJIpjv3hfBDkSDcWJA"
16
+ os.environ["TAVILY_API_KEY"] = "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"
17
  # ✅ Initialize Google Gemini Model
18
  llm = ChatGoogleGenerativeAI(model="gemini-2.0-flash")
19
  # ✅ Define Search Tool
20
+ # search_tool = DuckDuckGoSearchRun(region="uk-en", safe="strict")
21
+ search_tool = TavilySearch(region="uk-en", safe="strict")
22
  app = FastAPI()
23
  class ContentState(Dict):
24
  idea: str
 
69
  research_query = f"{state['idea']}-{state['company_name']} - {state['services']}"
70
  research_data = search_tool.run(research_query )
71
  state.update({"research_data": research_data})
72
+ print("research_done",research_data)
73
  return state
74
 
75
  # ✅ SEO Optimization Task
 
1150
  })
1151
 
1152
  return state
1153
+ def humanize(state: ContentState) -> ContentState:
1154
+ prompts={
1155
+ "home_page": f"""You are a highly skilled British writer with a natural, engaging, and conversational tone. Your task is to refine the given content so that it reads as if written entirely by a human—captivating, unpredictable, and emotionally rich. Ensure the text aligns with UK grammar, spelling, and style, making it feel authentic for a UK audience.
1156
+ Key Instructions:
1157
+ ✅ Do not add introductions, summaries, or extra commentary. Only rewrite the content naturally.
1158
+ ✅ Use British spelling and grammar—e.g., 'colour' instead of 'color,' 'analyse' instead of 'analyze.'
1159
+ ✅ Inject personality subtly through warmth, occasional wit, and natural phrasing. Use contractions (e.g., 'it’s,' 'we’re') to maintain a conversational flow.
1160
+ ✅ Ensure cultural relevance by weaving in British terms, idioms, or references where appropriate—e.g., 'petrol station' instead of 'gas station,' 'football' instead of 'soccer,' or phrases like 'a bit of a faff' or 'sorted.'
1161
+ ✅ Avoid robotic phrasing by reworking generic or stiff sentences into something more human and engaging.
1162
+ ✅ Introduce emotional nuance with subtle enthusiasm, curiosity, or personal insights—e.g., 'I reckon,' 'It’s hard not to smile when,' or 'You’d think it’d be simple, but...'
1163
+ ✅ Vary sentence structures and lengths for a human-like rhythm. Mix short, punchy sentences with longer, descriptive ones—e.g., 'It’s brilliant. No two ways about it. But then you dig a little deeper, and the real magic starts to shine through.'
1164
+ ✅ Use everyday language that feels authentic to a British writer, but allow nuanced or vivid words where they add depth or emotion.
1165
+ ✅ Engage the reader with rhetorical devices like analogies, examples, or questions—e.g., 'Ever wondered why...?' or 'It’s like finding a tenner in your old coat.'
1166
+ ✅ Format for readability with short paragraphs, and use bold or italics for emphasis where natural. Avoid overusing bullet points unless they suit the content.
1167
+ ✅ Avoid overly promotional hype, but feel free to express natural excitement or emphasis when it fits—e.g., 'It’s honestly quite impressive' instead of 'It revolutionizes everything.'
1168
+ Your goal is to refine the text while keeping its original meaning intact, ensuring it feels natural, engaging, and authentic to a human reader—especially one familiar with British culture and language. Focus on creating a voice that resonates, not just on passing detection tools.
1169
+ Here’s the content to refine:
1170
+ {state["home_page"]}.
1171
+ """,
1172
+ "about_us_page": f"""You are a highly skilled British writer with a natural, engaging, and conversational tone. Your task is to refine the given content so that it reads as if written entirely by a human—captivating, unpredictable, and emotionally rich. Ensure the text aligns with UK grammar, spelling, and style, making it feel authentic for a UK audience.
1173
+ Key Instructions:
1174
+ ✅ Do not add introductions, summaries, or extra commentary. Only rewrite the content naturally.
1175
+ ✅ Use British spelling and grammar—e.g., 'colour' instead of 'color,' 'analyse' instead of 'analyze.'
1176
+ ✅ Inject personality subtly through warmth, occasional wit, and natural phrasing. Use contractions (e.g., 'it’s,' 'we’re') to maintain a conversational flow.
1177
+ ✅ Ensure cultural relevance by weaving in British terms, idioms, or references where appropriate—e.g., 'petrol station' instead of 'gas station,' 'football' instead of 'soccer,' or phrases like 'a bit of a faff' or 'sorted.'
1178
+ ✅ Avoid robotic phrasing by reworking generic or stiff sentences into something more human and engaging.
1179
+ ✅ Introduce emotional nuance with subtle enthusiasm, curiosity, or personal insights—e.g., 'I reckon,' 'It’s hard not to smile when,' or 'You’d think it’d be simple, but...'
1180
+ ✅ Vary sentence structures and lengths for a human-like rhythm. Mix short, punchy sentences with longer, descriptive ones—e.g., 'It’s brilliant. No two ways about it. But then you dig a little deeper, and the real magic starts to shine through.'
1181
+ ✅ Use everyday language that feels authentic to a British writer, but allow nuanced or vivid words where they add depth or emotion.
1182
+ ✅ Engage the reader with rhetorical devices like analogies, examples, or questions—e.g., 'Ever wondered why...?' or 'It’s like finding a tenner in your old coat.'
1183
+ ✅ Format for readability with short paragraphs, and use bold or italics for emphasis where natural. Avoid overusing bullet points unless they suit the content.
1184
+ ✅ Avoid overly promotional hype, but feel free to express natural excitement or emphasis when it fits—e.g., 'It’s honestly quite impressive' instead of 'It revolutionizes everything.'
1185
+ Your goal is to refine the text while keeping its original meaning intact, ensuring it feels natural, engaging, and authentic to a human reader—especially one familiar with British culture and language. Focus on creating a voice that resonates, not just on passing detection tools.
1186
+ Here’s the content to refine:
1187
+ {state["about_us_page"]}.
1188
+ """,
1189
+ "service_page": f"""You are a highly skilled British writer with a natural, engaging, and conversational tone. Your task is to refine the given content so that it reads as if written entirely by a human—captivating, unpredictable, and emotionally rich. Ensure the text aligns with UK grammar, spelling, and style, making it feel authentic for a UK audience.
1190
+ Key Instructions:
1191
+ ✅ Do not add introductions, summaries, or extra commentary. Only rewrite the content naturally.
1192
+ ✅ Use British spelling and grammar—e.g., 'colour' instead of 'color,' 'analyse' instead of 'analyze.'
1193
+ ✅ Inject personality subtly through warmth, occasional wit, and natural phrasing. Use contractions (e.g., 'it’s,' 'we’re') to maintain a conversational flow.
1194
+ ✅ Ensure cultural relevance by weaving in British terms, idioms, or references where appropriate—e.g., 'petrol station' instead of 'gas station,' 'football' instead of 'soccer,' or phrases like 'a bit of a faff' or 'sorted.'
1195
+ ✅ Avoid robotic phrasing by reworking generic or stiff sentences into something more human and engaging.
1196
+ ✅ Introduce emotional nuance with subtle enthusiasm, curiosity, or personal insights—e.g., 'I reckon,' 'It’s hard not to smile when,' or 'You’d think it’d be simple, but...'
1197
+ ✅ Vary sentence structures and lengths for a human-like rhythm. Mix short, punchy sentences with longer, descriptive ones—e.g., 'It’s brilliant. No two ways about it. But then you dig a little deeper, and the real magic starts to shine through.'
1198
+ ✅ Use everyday language that feels authentic to a British writer, but allow nuanced or vivid words where they add depth or emotion.
1199
+ ✅ Engage the reader with rhetorical devices like analogies, examples, or questions—e.g., 'Ever wondered why...?' or 'It’s like finding a tenner in your old coat.'
1200
+ ✅ Format for readability with short paragraphs, and use bold or italics for emphasis where natural. Avoid overusing bullet points unless they suit the content.
1201
+ ✅ Avoid overly promotional hype, but feel free to express natural excitement or emphasis when it fits—e.g., 'It’s honestly quite impressive' instead of 'It revolutionizes everything.'
1202
+ Your goal is to refine the text while keeping its original meaning intact, ensuring it feels natural, engaging, and authentic to a human reader—especially one familiar with British culture and language. Focus on creating a voice that resonates, not just on passing detection tools.
1203
+ Here’s the content to refine:
1204
+ {state["service_page"]}.
1205
+ """,
1206
+ "individual_service_page": f"""You are a highly skilled British writer with a natural, engaging, and conversational tone. Your task is to refine the given content so that it reads as if written entirely by a human—captivating, unpredictable, and emotionally rich. Ensure the text aligns with UK grammar, spelling, and style, making it feel authentic for a UK audience.
1207
+ Key Instructions:
1208
+ ✅ Do not add introductions, summaries, or extra commentary. Only rewrite the content naturally.
1209
+ ✅ Use British spelling and grammar—e.g., 'colour' instead of 'color,' 'analyse' instead of 'analyze.'
1210
+ ✅ Inject personality subtly through warmth, occasional wit, and natural phrasing. Use contractions (e.g., 'it’s,' 'we’re') to maintain a conversational flow.
1211
+ ✅ Ensure cultural relevance by weaving in British terms, idioms, or references where appropriate—e.g., 'petrol station' instead of 'gas station,' 'football' instead of 'soccer,' or phrases like 'a bit of a faff' or 'sorted.'
1212
+ ✅ Avoid robotic phrasing by reworking generic or stiff sentences into something more human and engaging.
1213
+ ✅ Introduce emotional nuance with subtle enthusiasm, curiosity, or personal insights—e.g., 'I reckon,' 'It’s hard not to smile when,' or 'You’d think it’d be simple, but...'
1214
+ ✅ Vary sentence structures and lengths for a human-like rhythm. Mix short, punchy sentences with longer, descriptive ones—e.g., 'It’s brilliant. No two ways about it. But then you dig a little deeper, and the real magic starts to shine through.'
1215
+ ✅ Use everyday language that feels authentic to a British writer, but allow nuanced or vivid words where they add depth or emotion.
1216
+ ✅ Engage the reader with rhetorical devices like analogies, examples, or questions—e.g., 'Ever wondered why...?' or 'It’s like finding a tenner in your old coat.'
1217
+ ✅ Format for readability with short paragraphs, and use bold or italics for emphasis where natural. Avoid overusing bullet points unless they suit the content.
1218
+ ✅ Avoid overly promotional hype, but feel free to express natural excitement or emphasis when it fits—e.g., 'It’s honestly quite impressive' instead of 'It revolutionizes everything.'
1219
+ Your goal is to refine the text while keeping its original meaning intact, ensuring it feels natural, engaging, and authentic to a human reader—especially one familiar with British culture and language. Focus on creating a voice that resonates, not just on passing detection tools.
1220
+ Here’s the content to refine:
1221
+ {state["individual_service_page"]}.
1222
+ """,
1223
+ "service_area_page": f"""You are a highly skilled British writer with a natural, engaging, and conversational tone. Your task is to refine the given content so that it reads as if written entirely by a human—captivating, unpredictable, and emotionally rich. Ensure the text aligns with UK grammar, spelling, and style, making it feel authentic for a UK audience.
1224
+ Key Instructions:
1225
+ ✅ Do not add introductions, summaries, or extra commentary. Only rewrite the content naturally.
1226
+ ✅ Use British spelling and grammar—e.g., 'colour' instead of 'color,' 'analyse' instead of 'analyze.'
1227
+ ✅ Inject personality subtly through warmth, occasional wit, and natural phrasing. Use contractions (e.g., 'it’s,' 'we’re') to maintain a conversational flow.
1228
+ ✅ Ensure cultural relevance by weaving in British terms, idioms, or references where appropriate—e.g., 'petrol station' instead of 'gas station,' 'football' instead of 'soccer,' or phrases like 'a bit of a faff' or 'sorted.'
1229
+ ✅ Avoid robotic phrasing by reworking generic or stiff sentences into something more human and engaging.
1230
+ ✅ Introduce emotional nuance with subtle enthusiasm, curiosity, or personal insights—e.g., 'I reckon,' 'It’s hard not to smile when,' or 'You’d think it’d be simple, but...'
1231
+ ✅ Vary sentence structures and lengths for a human-like rhythm. Mix short, punchy sentences with longer, descriptive ones—e.g., 'It’s brilliant. No two ways about it. But then you dig a little deeper, and the real magic starts to shine through.'
1232
+ ✅ Use everyday language that feels authentic to a British writer, but allow nuanced or vivid words where they add depth or emotion.
1233
+ ✅ Engage the reader with rhetorical devices like analogies, examples, or questions—e.g., 'Ever wondered why...?' or 'It’s like finding a tenner in your old coat.'
1234
+ ✅ Format for readability with short paragraphs, and use bold or italics for emphasis where natural. Avoid overusing bullet points unless they suit the content.
1235
+ ✅ Avoid overly promotional hype, but feel free to express natural excitement or emphasis when it fits—e.g., 'It’s honestly quite impressive' instead of 'It revolutionizes everything.'
1236
+ Your goal is to refine the text while keeping its original meaning intact, ensuring it feels natural, engaging, and authentic to a human reader—especially one familiar with British culture and language. Focus on creating a voice that resonates, not just on passing detection tools.
1237
+ Here’s the content to refine:
1238
+ {state["service_area_page"]}.
1239
+ """,
1240
+
1241
+ }
1242
+ humanized_content = {key: llm.invoke(prompt).content.strip() for key, prompt in prompts.items()}
1243
+ state.update(humanized_content)
1244
+ print("humannn")
1245
+ return state
1246
+
1247
  def upload_file(state: ContentState) -> ContentState:
1248
  uploaded_file = state["file_path"] # Extract uploaded file
1249