amir22010 commited on
Commit
494ef37
·
verified ·
1 Parent(s): c08bb9b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -11
app.py CHANGED
@@ -3,13 +3,39 @@ from llama_cpp import Llama
3
  import os
4
  from groq import Groq
5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
  client = Groq(
7
  api_key=os.getenv("GROQ_API_KEY"),
8
  )
9
 
10
  llm = Llama.from_pretrained(
11
- repo_id="amir22010/fine_tuned_product_marketing_email_gemma_2_9b_q4_k_m",
12
- filename="unsloth.Q4_K_M.gguf",
13
  cache_dir=os.path.abspath(os.getcwd()),
14
  n_ctx=2048,
15
  n_batch=126,
@@ -17,7 +43,6 @@ llm = Llama.from_pretrained(
17
  )
18
 
19
  #guardrail model
20
- #guard_llm = "llama-guard-3-8b"
21
  guard_llm = "llama-3.1-8b-instant"
22
 
23
  #marketing prompt
@@ -71,19 +96,25 @@ async def greet(product,description):
71
  ]
72
  response = client.chat.completions.create(model=guard_llm, messages=messages, temperature=0)
73
  if response.choices[0].message.content != "not moderated":
74
- a_list = ["Sorry can't proceed for generate marketing email!. Your content needs to be moderated first."]
75
- for i in a_list:
76
- yield i
 
 
 
 
77
  else:
 
 
78
  output = llm.create_chat_completion(
79
  messages=[
80
  {
81
  "role": "system",
82
- "content": "Your go-to Email Marketing Guru - I'm here to help you craft compelling campaigns, boost conversions, and take your business to the next level.",
83
  },
84
  {"role": "user", "content": user_reques},
85
  ],
86
- max_tokens=4096,
87
  temperature=0.7,
88
  stream=True
89
  )
@@ -91,8 +122,12 @@ async def greet(product,description):
91
  for chunk in output:
92
  delta = chunk['choices'][0]['delta']
93
  if 'content' in delta:
94
- partial_message = partial_message + delta.get('content', '')
95
- yield partial_message
 
 
 
 
96
 
97
- demo = gr.Interface(fn=greet, inputs=["text","text"], outputs="text", concurrency_limit=10)
98
  demo.launch()
 
3
  import os
4
  from groq import Groq
5
 
6
+ #tts
7
+ from balacoon_tts import TTS
8
+ from threading import Lock
9
+ from io import BytesIO
10
+ from huggingface_hub import hf_hub_download, list_repo_files
11
+
12
+ #tts cpu model
13
+ tts_model_str = "en_us_hifi_jets_cpu.addon"
14
+
15
+ model_repo_dir = "/data"
16
+ for name in list_repo_files(repo_id="balacoon/tts"):
17
+ if name == tts_model_str:
18
+ if not os.path.isfile(os.path.join(model_repo_dir, name)):
19
+ hf_hub_download(
20
+ repo_id="balacoon/tts",
21
+ filename=name,
22
+ local_dir=model_repo_dir,
23
+ )
24
+
25
+ #tts speaker
26
+ speaker_str = "92"
27
+
28
+ # locker that disallow access to the tts object from more then one thread
29
+ locker = Lock()
30
+
31
+ #client
32
  client = Groq(
33
  api_key=os.getenv("GROQ_API_KEY"),
34
  )
35
 
36
  llm = Llama.from_pretrained(
37
+ repo_id="amir22010/fine_tuned_product_marketing_email_gemma_2_9b_q4_k_m", #custom fine tuned model
38
+ filename="unsloth.Q4_K_M.gguf", #model file name
39
  cache_dir=os.path.abspath(os.getcwd()),
40
  n_ctx=2048,
41
  n_batch=126,
 
43
  )
44
 
45
  #guardrail model
 
46
  guard_llm = "llama-3.1-8b-instant"
47
 
48
  #marketing prompt
 
96
  ]
97
  response = client.chat.completions.create(model=guard_llm, messages=messages, temperature=0)
98
  if response.choices[0].message.content != "not moderated":
99
+ audio_stream = BytesIO()
100
+ tts = TTS(os.path.join(model_repo_dir, tts_model_str))
101
+ a_list = ["Sorry, I can't proceed for generating marketing email. Your content needs to be moderated first. Thank you!"]
102
+ with locker:
103
+ audio_stream.write(tts.synthesize(a_list[0], speaker_str))
104
+ audio_stream.seek(0)
105
+ yield audio_stream
106
  else:
107
+ audio_stream = BytesIO()
108
+ tts = TTS(os.path.join(model_repo_dir, tts_model_str))
109
  output = llm.create_chat_completion(
110
  messages=[
111
  {
112
  "role": "system",
113
+ "content": "Your go-to Email Marketing Guru - I'm here to help you craft short and concise compelling campaigns, boost conversions, and take your business to the next level.",
114
  },
115
  {"role": "user", "content": user_reques},
116
  ],
117
+ max_tokens=2048,
118
  temperature=0.7,
119
  stream=True
120
  )
 
122
  for chunk in output:
123
  delta = chunk['choices'][0]['delta']
124
  if 'content' in delta:
125
+ with locker:
126
+ audio_stream.write(tts.synthesize(delta.get('content', ''), speaker_str))
127
+ # partial_message = partial_message + delta.get('content', '')
128
+ # yield partial_message
129
+ audio_stream.seek(0)
130
+ yield audio_stream
131
 
132
+ demo = gr.Interface(fn=greet, inputs=["text","text"], outputs=gr.Audio(), concurrency_limit=10)
133
  demo.launch()