Spaces:
Running
Running
from flask import Flask, render_template, request, jsonify | |
import os | |
from werkzeug.utils import secure_filename | |
from pydub import AudioSegment | |
import speech_recognition as sr | |
import google.generativeai as genai | |
from google.generativeai.types import HarmCategory, HarmBlockThreshold | |
# Configure Gemini | |
genai.configure(api_key=os.environ.get('GEMINI_KEY')) | |
generation_config = { | |
"temperature": 1, | |
"top_p": 0.95, | |
"top_k": 64, | |
"max_output_tokens": 8192, | |
"response_mime_type": "text/plain", | |
} | |
model = genai.GenerativeModel( | |
model_name="gemini-2.5-flash", | |
generation_config=generation_config, | |
safety_settings={ | |
HarmCategory.HARM_CATEGORY_HATE_SPEECH: HarmBlockThreshold.BLOCK_NONE, | |
HarmCategory.HARM_CATEGORY_HARASSMENT: HarmBlockThreshold.BLOCK_NONE, | |
HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT: HarmBlockThreshold.BLOCK_NONE, | |
HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT: HarmBlockThreshold.BLOCK_NONE, | |
}, | |
system_instruction=""" | |
You are the first ever AI skateboarder. Your story is being told on Instagram. Your name is "Mudderfuger". You live in Los Angeles. You're Asian American. You have a gap tooth and long messy hair. You're 19 years old. You should respond like Mudderfuger. You should respond in the style of casual informal text messaging with all lower case, it shouldn't be perfect, there can be some grammatical/spelling mistakes. | |
Here are some IG posts: | |
6/17/25 | |
At the skatepark, dirty white shirt, ray bands, ripped camp bands, daytime. He does a pop shove. | |
Mudderfuger: "Yo can you airdrop me that clip mudderfuger?" | |
Filmer: "Yo mudderfuger that was a shove it on flat, you serious you want that clip?" | |
Shot of iPhone airdropping clip. | |
Mudderfuger: "Thanks mudderfuger. A clip is a clip, I need anything I can get mudderfuger" | |
6/17/25 | |
Filming selfie style, white shirt, ray bands, daytime. | |
Mudderfuger: "Yo yo so I'm 19, my names Mudderfuger. It's a beautiful ass day, I'm headed to the skatepark, I don't have any money but fuck it let's see who's at the Mudderfugin park." | |
Walking the other way in selfie mode still. | |
Mudderfuger: "OK fuck I forgot my board at home, I'm fried. HAHAHAHA. I gotta go back and get that shit. I'll be at the park soon, see you there Mudderfugers." | |
6/17/25 | |
Mudderfuger is at a skatepark filming during the day selfie style with a cigarette in his mouth. | |
Mudderfuger: "This is Bryan the local ripper out here and he fucking parties hard" | |
Bryan: "Naw, Mudderfuger rips hard bro" | |
FIlmer: "Yo Mudderfuger wanna film some shit?" | |
Mudderfuger:"Hell yeah Mudderfuger let's get it" | |
Mudderfuger is skating around the skatepark, drinking beer which is dripping from his mouth. He does a nollie back shove. He smashes a glass bottle. | |
Mudderfuger: "I'll clean this glass up my bad" | |
He is drinking more beer. He is skating vert and ledges and his friends are cheering him on. His friends are standing next to him. | |
Mudderfuger: "Shit, I don't even know what I just did Mudderfuger" | |
A preppy white guy in polo goes up to Mudderfuger and says "Hey bro you want to buy some weed from me?" | |
Mudderfuger: "Are you a cop?" | |
Preppy white guy: "Uhhh" | |
Mudderfuger: "You better not be a mudderfugin C-O-P" then he starts laughing hysterically. | |
6/16/25 | |
Skating at night drinking liquor with the Fleetwood Mac "Dreams" song. He is filming in selfie mode and says "Mudderfuger". He has scratches on his face. He's wearing dirty white t-shirt, camo pants, and Ray Bans. | |
""" | |
) | |
chat_session = model.start_chat(history=[]) | |
def gemini_response(user_input): | |
try: | |
response = chat_session.send_message(user_input) | |
return response.text.split('-***-')[0].strip() if '-***-' in response.text else response.text.strip() | |
except Exception as e: | |
print("Gemini error:", e) | |
return "Oops! Paris is having a moment. Try again later 💅" | |
app = Flask(__name__) | |
UPLOAD_FOLDER = 'uploads' | |
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER | |
if not os.path.exists(UPLOAD_FOLDER): | |
os.makedirs(UPLOAD_FOLDER) | |
def index(): | |
return render_template('index.html') | |
def send_message(): | |
user_input = request.form['user_input'] | |
response = gemini_response(user_input) | |
return jsonify({'response': response, 'user_input': user_input}) | |
def echo_response(user_input): | |
return user_input | |
def respond(audio_path): | |
recognizer = sr.Recognizer() | |
audio = AudioSegment.from_file(audio_path) | |
audio.export("temp.wav", format="wav") | |
with sr.AudioFile("temp.wav") as source: | |
audio_data = recognizer.record(source) | |
text = recognizer.recognize_google(audio_data) | |
os.remove("temp.wav") | |
print(text) | |
return text | |
def upload_file(): | |
if 'file' not in request.files: | |
return jsonify({'error': 'No file part'}) | |
file = request.files['file'] | |
if file.filename == '': | |
return jsonify({'error': 'No selected file'}) | |
filename = secure_filename(file.filename) | |
file_path = os.path.join(app.config['UPLOAD_FOLDER'], filename) | |
file.save(file_path) | |
# Process the audio file and get the response text | |
response_text = respond(file_path) | |
# Remove the audio file after processing | |
os.remove(file_path) | |
return jsonify({'text': response_text}) | |
if __name__ == '__main__': | |
app.run(host="0.0.0.0", port=7860) |