thomasanto7001 commited on
Commit
ac42869
·
verified ·
1 Parent(s): 47a4f10

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -23
app.py CHANGED
@@ -1,40 +1,50 @@
1
- # Tech Trends Curator with Audio
2
 
 
 
3
  import gradio as gr
4
- from groq import Groq
5
  from gtts import gTTS
6
- import uuid
7
- import os
8
 
9
- client = Groq(api_key=os.environ["GROQ_API_KEY"])
 
10
 
11
- def get_tech_trends_with_audio(user_input, history):
12
- system_prompt = {
13
- "role": "system",
14
- "content": (
15
- "You are a tech trend curator. Summarize the latest AI tools, GitHub projects, "
16
- "and startup news in an engaging tone. If asked for fake/funny trends, be creative and witty."
17
- )
18
  }
19
 
20
- messages = [system_prompt, {"role": "user", "content": user_input}]
 
 
 
 
 
 
 
 
 
21
 
22
- response = client.chat.completions.create(
23
- model="llama3-8b-8192",
24
- messages=messages
25
- )
26
 
27
- output_text = response.choices[0].message.content
 
 
28
 
29
- # Convert text to speech
30
  tts = gTTS(output_text)
31
  filename = f"/tmp/{uuid.uuid4()}.mp3"
32
  tts.save(filename)
33
 
34
  return output_text, filename
35
 
36
- # Define Gradio Interface
37
- interface = gr.Interface(
38
  fn=get_tech_trends_with_audio,
39
  inputs=gr.Textbox(placeholder="Ask for top trends, tools, or fake news", label="Your Question"),
40
  outputs=[
@@ -51,5 +61,5 @@ interface = gr.Interface(
51
  theme="soft"
52
  )
53
 
54
- # Launch the app
55
- interface.launch(share=True)
 
1
+ # Tech Trends Curator with Audio (Hugging Face Compatible)
2
 
3
+ import os
4
+ import uuid
5
  import gradio as gr
6
+ import requests
7
  from gtts import gTTS
 
 
8
 
9
+ # Fetch API key from environment
10
+ GROQ_API_KEY = os.environ.get("GROQ_API_KEY")
11
 
12
+ # Function to query Groq LLM via REST API
13
+ def query_groq(user_input):
14
+ url = "https://api.groq.com/openai/v1/chat/completions"
15
+ headers = {
16
+ "Authorization": f"Bearer {GROQ_API_KEY}",
17
+ "Content-Type": "application/json"
 
18
  }
19
 
20
+ data = {
21
+ "model": "llama3-8b-8192",
22
+ "messages": [
23
+ {"role": "system", "content": (
24
+ "You are a tech trend curator. Summarize the latest AI tools, GitHub projects, "
25
+ "and startup news in an engaging tone. If asked for fake/funny trends, be creative and witty."
26
+ )},
27
+ {"role": "user", "content": user_input}
28
+ ]
29
+ }
30
 
31
+ response = requests.post(url, headers=headers, json=data)
32
+ result = response.json()
33
+ return result["choices"][0]["message"]["content"]
 
34
 
35
+ # Main function for chatbot + audio generation
36
+ def get_tech_trends_with_audio(user_input, history):
37
+ output_text = query_groq(user_input)
38
 
39
+ # Generate audio
40
  tts = gTTS(output_text)
41
  filename = f"/tmp/{uuid.uuid4()}.mp3"
42
  tts.save(filename)
43
 
44
  return output_text, filename
45
 
46
+ # Gradio interface
47
+ demo = gr.Interface(
48
  fn=get_tech_trends_with_audio,
49
  inputs=gr.Textbox(placeholder="Ask for top trends, tools, or fake news", label="Your Question"),
50
  outputs=[
 
61
  theme="soft"
62
  )
63
 
64
+ # Launch app
65
+ demo.launch(share=True)