HassanDataSci commited on
Commit
dbd0a3c
·
verified ·
1 Parent(s): 9da7cc2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +103 -0
app.py CHANGED
@@ -0,0 +1,103 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import speech_recognition as sr
3
+ from transformers import pipeline
4
+ from gtts import gTTS
5
+ import requests
6
+ import os
7
+
8
+ # Initialize the text generation pipeline with GPT-2
9
+ generator = pipeline('text-generation', model='gpt2')
10
+
11
+ # Function to generate a Trump-like response
12
+ def generate_response(prompt):
13
+ # This is your original `generate_response` function
14
+ response = generator(
15
+ prompt,
16
+ max_length=100, # Adjust length as needed
17
+ num_return_sequences=1, # Generate only one response
18
+ temperature=0.7, # Adjust for more randomness (higher) or more coherence (lower)
19
+ )
20
+ return response[0]['generated_text']
21
+
22
+ # Uberduck API keys - Replace with your actual Uberduck API credentials
23
+ UBERDUCK_API_KEY = 'your_uberduck_api_key' # Replace with your actual Uberduck API Key
24
+ UBERDUCK_API_SECRET = 'your_uberduck_api_secret' # Replace with your actual Uberduck API Secret
25
+
26
+ # Function to generate audio using Uberduck API
27
+ def generate_audio_uberduck(text):
28
+ # This is your original Uberduck API function
29
+ url = "https://api.uberduck.ai/speak"
30
+ headers = {
31
+ "Accept": "application/json",
32
+ "uberduck-api-key": UBERDUCK_API_KEY,
33
+ "uberduck-api-secret": UBERDUCK_API_SECRET
34
+ }
35
+ json_data = {
36
+ "speech": text,
37
+ "voice": "donald-trump" # Use "donald-trump" voice in Uberduck (if available)
38
+ }
39
+
40
+ # Send a request to Uberduck's API to generate audio
41
+ response = requests.post(url, headers=headers, json=json_data)
42
+ if response.status_code == 200:
43
+ audio_url = response.json().get("path")
44
+ # Download the audio file from the URL
45
+ audio_data = requests.get(audio_url).content
46
+ with open("output.mp3", "wb") as file:
47
+ file.write(audio_data)
48
+ return "output.mp3"
49
+ else:
50
+ st.write("Error generating audio:", response.text)
51
+ return None
52
+
53
+ # Alternative function to generate audio using gTTS
54
+ def generate_audio_gtts(text):
55
+ tts = gTTS(text=text, lang="en")
56
+ audio_path = "output.mp3"
57
+ tts.save(audio_path)
58
+ return audio_path
59
+
60
+ # Function to transcribe audio to text
61
+ def transcribe_audio(file_path):
62
+ recognizer = sr.Recognizer()
63
+ try:
64
+ with sr.AudioFile(file_path) as source:
65
+ audio = recognizer.record(source)
66
+ text = recognizer.recognize_google(audio)
67
+ return text
68
+ except Exception as e:
69
+ st.write("Error transcribing audio:", str(e))
70
+ return None
71
+
72
+ # Streamlit app UI
73
+ st.title("Trump-like Voice Assistant")
74
+ st.write("Record or upload an audio clip and receive a 'Trump-style' response.")
75
+
76
+ # Upload or record audio
77
+ uploaded_audio = st.file_uploader("Upload an audio file (wav format preferred)", type=["wav", "mp3"])
78
+
79
+ if uploaded_audio is not None:
80
+ # Save the uploaded audio file temporarily
81
+ audio_path = "input_audio.wav"
82
+ with open(audio_path, "wb") as f:
83
+ f.write(uploaded_audio.getbuffer())
84
+
85
+ # Transcribe audio to text
86
+ user_input = transcribe_audio(audio_path)
87
+ if user_input:
88
+ st.write(f"Transcribed text: {user_input}")
89
+
90
+ # Generate Trump-like response
91
+ trump_prompt = f"As a bold and confident leader, respond to: {user_input}"
92
+ trump_response = generate_response(trump_prompt)
93
+ st.write("Trump-like Assistant:", trump_response)
94
+
95
+ # Generate audio response (choose either Uberduck or gTTS)
96
+ # Uncomment one of the following two lines depending on your preferred TTS provider
97
+ audio_output_path = generate_audio_uberduck(trump_response) # Uberduck
98
+ # audio_output_path = generate_audio_gtts(trump_response) # gTTS as fallback
99
+
100
+ if audio_output_path:
101
+ audio_file = open(audio_output_path, "rb")
102
+ audio_bytes = audio_file.read()
103
+ st.audio(audio_bytes, format="audio/mp3")