728x90
반응형
SMALL
1. 아래와 같이 코딩 후 실행합니다.
>아래 주석(while문)을 풀고 실행시 마이크에 대고 한국어로 얘기하면 실시간으로 감정분석 결과를 얻을 수 있습니다.
import google.generativeai as genai
import speech_recognition as sr
from config import *
genai.configure(api_key=API_KEY)
# model = genai.GenerativeModel("gemini-1.5-flash")
model = genai.GenerativeModel("gemini-2.0-flash-exp")
response = model.generate_content("Explain how AI works")
def analyze_emotion(text):
prompt = f"이 텍스트의 감정을 분석하고 간단한 요약을 제공합니다: '{text}'"
response = model.generate_content(prompt)
return response.text
def generate_response(emotion, text):
prompt = f"감정 '{emotion}'과 맥락 '{text}'가 주어지면 공감적인 반응을 일으킵니다:"
response = model.generate_content(prompt)
return response.text
def speech_to_text():
recognizer = sr.Recognizer()
with sr.Microphone() as source:
print("말씀해주세요...")
audio = recognizer.listen(source)
try:
text = recognizer.recognize_google(audio, language="ko-KR")
return text
except sr.UnknownValueError:
print("음성을 인식할 수 없습니다.")
return None
# 메인 실행 부분
if __name__ == "__main__":
input_text = "당신을 사랑합니다."
if input_text:
print(f"인식된 텍스트: {input_text}")
emotion = analyze_emotion(input_text)
print(f"감지된 감정: {emotion}")
response = generate_response(emotion, input_text)
print(f"AI 응답: {response}")
# while True:
# # input_text = speech_to_text()
# input_text = "i love you"
# if input_text:
# print(f"인식된 텍스트: {input_text}")
# emotion = analyze_emotion(input_text)
# print(f"감지된 감정: {emotion}")
# response = generate_response(emotion, input_text)
# print(f"AI 응답: {response}")
2.아래와 같이 응답결과를 얻을 수 있습니다.
>실시간 감정분석 시 speech_recognition 이 모듈이 정상적으로 작동하지 않을 수 있습니다. 이런 경우 다른 모듈을 사용해야 합니다.
728x90
반응형
LIST
'퀵포스팅 > 제미나이2.0 ai api로 할 수 있는 것들' 카테고리의 다른 글
제미나이 2.0 ai api로 할 수 있는 것들 - 8.오디오 분석하기 (0) | 2024.12.22 |
---|---|
제미나이 2.0 ai api로 할 수 있는 것들 - 7.AI Studio api의 다양한 모델들 (0) | 2024.12.22 |
제미나이 2.0 ai api로 할 수 있는 것들 - 5.실시간 번역 (0) | 2024.12.22 |
제미나이 2.0 ai api로 할 수 있는 것들 - 4.여행계획 짜기 (3) | 2024.12.22 |
제미나이 2.0 ai api로 할 수 있는 것들 - 3.동영상에 대한 설명 얻기 (1) | 2024.12.22 |