thomasanto7001 commited on
Commit
e8813fc
·
verified ·
1 Parent(s): 8c8b23a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -34
app.py CHANGED
@@ -1,4 +1,4 @@
1
- # Tech Trends Curator with Audio (Hugging Face Compatible)
2
 
3
  import os
4
  import uuid
@@ -10,7 +10,7 @@ from gtts import gTTS
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}",
@@ -19,47 +19,48 @@ def query_groq(user_input):
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=[
51
- gr.Textbox(label="Trend Summary"),
52
- gr.Audio(label="Audio Summary")
53
- ],
54
- title="📈 Tech Trends Curator",
55
- description="Get AI, GitHub, and startup trend summaries — now with audio!",
56
- examples=[
57
- ["Top 3 AI tools this week"],
58
- ["Funny fake trends in tech"],
59
- ["What’s hot on GitHub in AI?"]
60
- ],
61
- theme="soft"
62
- )
63
 
64
- # Launch app
65
  demo.launch(share=True)
 
1
+ # Tech Trends Curator Chatbot with Audio (Hugging Face Compatible)
2
 
3
  import os
4
  import uuid
 
10
  GROQ_API_KEY = os.environ.get("GROQ_API_KEY")
11
 
12
  # Function to query Groq LLM via REST API
13
+ def query_groq(messages):
14
  url = "https://api.groq.com/openai/v1/chat/completions"
15
  headers = {
16
  "Authorization": f"Bearer {GROQ_API_KEY}",
 
19
 
20
  data = {
21
  "model": "llama3-8b-8192",
22
+ "messages": messages
 
 
 
 
 
 
23
  }
24
 
25
  response = requests.post(url, headers=headers, json=data)
26
  result = response.json()
27
  return result["choices"][0]["message"]["content"]
28
 
29
+ # Chatbot function with audio output
30
+ def chat_with_audio(user_input, history):
31
+ system_prompt = {
32
+ "role": "system",
33
+ "content": (
34
+ "You are a tech trend curator. Summarize the latest AI tools, GitHub projects, "
35
+ "and startup news in an engaging tone. If asked for fake/funny trends, be creative and witty."
36
+ )
37
+ }
38
+
39
+ messages = [system_prompt] + [{"role": "user", "content": inp} if i % 2 == 0 else {"role": "assistant", "content": inp} for i, inp in enumerate(sum(history, []))]
40
+ messages.append({"role": "user", "content": user_input})
41
+
42
+ response_text = query_groq(messages)
43
 
44
  # Generate audio
45
+ tts = gTTS(response_text)
46
+ audio_file = f"/tmp/{uuid.uuid4()}.mp3"
47
+ tts.save(audio_file)
48
+
49
+ return response_text, history + [[user_input, response_text]], audio_file
50
+
51
+ # Gradio ChatInterface with audio output
52
+ with gr.Blocks(theme="soft") as demo:
53
+ gr.Markdown("# 📈 Tech Trends Curator\nChat about trending AI tools, GitHub projects, and startup news — with audio!")
54
+ chatbot = gr.Chatbot(height=400)
55
+ msg = gr.Textbox(placeholder="Ask for top trends, tools, or fake news")
56
+ audio = gr.Audio(label="Audio Summary")
57
+ state = gr.State([])
58
 
59
+ def user_message(user_input, history):
60
+ reply, new_history, audio_path = chat_with_audio(user_input, history)
61
+ return reply, new_history, audio_path
62
 
63
+ msg.submit(user_message, [msg, state], [chatbot, state, audio])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
64
 
65
+ # Launch the app
66
  demo.launch(share=True)