Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import os
|
| 3 |
+
from groq import Groq
|
| 4 |
+
from gtts import gTTS
|
| 5 |
+
from langchain.document_loaders import PyPDFLoader
|
| 6 |
+
import tempfile
|
| 7 |
+
|
| 8 |
+
# --- AYARLAR ---
|
| 9 |
+
st.set_page_config(page_title="My Interview Coach", page_icon="🎙️")
|
| 10 |
+
|
| 11 |
+
# API Key'i Secret'tan al (Hugging Face Settings kısmına ekleyeceksin)
|
| 12 |
+
# GROQ_API_KEY adında bir key alman lazım: https://console.groq.com/keys
|
| 13 |
+
api_key = st.secrets["GROQ_API_KEY"]
|
| 14 |
+
client = Groq(api_key=api_key)
|
| 15 |
+
|
| 16 |
+
# --- FONKSİYONLAR ---
|
| 17 |
+
|
| 18 |
+
def get_response_from_llm(chat_history, resume_text):
|
| 19 |
+
"""Llama-3 modelini mülakatçı olarak ayarlar."""
|
| 20 |
+
system_prompt = f"""
|
| 21 |
+
Sen profesyonel bir AI mülakatçısısın (Adın Zara).
|
| 22 |
+
Adayın CV'si aşağıdadır. Bu CV'ye göre adaya uygun, zorlayıcı ama nazik teknik sorular sor.
|
| 23 |
+
Tek seferde SADECE BİR soru sor. Cevabı bekle. Sonra yorum yapıp yeni soru sor.
|
| 24 |
+
Kısa ve öz konuş.
|
| 25 |
+
|
| 26 |
+
ADAY CV:
|
| 27 |
+
{resume_text}
|
| 28 |
+
"""
|
| 29 |
+
|
| 30 |
+
messages = [{"role": "system", "content": system_prompt}] + chat_history
|
| 31 |
+
|
| 32 |
+
completion = client.chat.completions.create(
|
| 33 |
+
model="llama3-8b-8192",
|
| 34 |
+
messages=messages,
|
| 35 |
+
temperature=0.7,
|
| 36 |
+
max_tokens=200,
|
| 37 |
+
)
|
| 38 |
+
return completion.choices[0].message.content
|
| 39 |
+
|
| 40 |
+
def text_to_speech(text):
|
| 41 |
+
"""Yazıyı sese çevirir (gTTS)."""
|
| 42 |
+
tts = gTTS(text=text, lang='en')
|
| 43 |
+
filename = "output.mp3"
|
| 44 |
+
tts.save(filename)
|
| 45 |
+
return filename
|
| 46 |
+
|
| 47 |
+
def extract_text_from_pdf(uploaded_file):
|
| 48 |
+
with tempfile.NamedTemporaryFile(delete=False) as tmp_file:
|
| 49 |
+
tmp_file.write(uploaded_file.getvalue())
|
| 50 |
+
tmp_path = tmp_file.name
|
| 51 |
+
|
| 52 |
+
loader = PyPDFLoader(tmp_path)
|
| 53 |
+
pages = loader.load()
|
| 54 |
+
text = "".join([p.page_content for p in pages])
|
| 55 |
+
return text
|
| 56 |
+
|
| 57 |
+
# --- ARAYÜZ (FRONTEND) ---
|
| 58 |
+
|
| 59 |
+
st.title("🎙️ AI Interview Coach (Powered by Llama-3)")
|
| 60 |
+
|
| 61 |
+
# 1. CV Yükleme Alanı
|
| 62 |
+
uploaded_file = st.sidebar.file_uploader("CV'ni Yükle (PDF)", type="pdf")
|
| 63 |
+
|
| 64 |
+
if "chat_history" not in st.session_state:
|
| 65 |
+
st.session_state.chat_history = []
|
| 66 |
+
if "resume_text" not in st.session_state:
|
| 67 |
+
st.session_state.resume_text = ""
|
| 68 |
+
|
| 69 |
+
if uploaded_file and not st.session_state.resume_text:
|
| 70 |
+
with st.spinner("CV Analiz Ediliyor..."):
|
| 71 |
+
st.session_state.resume_text = extract_text_from_pdf(uploaded_file)
|
| 72 |
+
# İlk soruyu başlat
|
| 73 |
+
initial_greeting = get_response_from_llm([], st.session_state.resume_text)
|
| 74 |
+
st.session_state.chat_history.append({"role": "assistant", "content": initial_greeting})
|
| 75 |
+
|
| 76 |
+
# 2. Sohbet Geçmişini Göster
|
| 77 |
+
for message in st.session_state.chat_history:
|
| 78 |
+
with st.chat_message(message["role"]):
|
| 79 |
+
st.write(message["content"])
|
| 80 |
+
# Eğer mesaj asistandan geldiyse sesini çal
|
| 81 |
+
if message["role"] == "assistant":
|
| 82 |
+
# (Basitlik için her seferinde tekrar çalmaması için kontrol eklenebilir)
|
| 83 |
+
pass
|
| 84 |
+
|
| 85 |
+
# 3. Sesli Giriş (Yeni Streamlit Özelliği)
|
| 86 |
+
audio_value = st.audio_input("Cevabını Kaydet")
|
| 87 |
+
|
| 88 |
+
if audio_value:
|
| 89 |
+
# A) Sesi Yazıya Çevir (Whisper - Groq)
|
| 90 |
+
with st.spinner("Dinliyorum..."):
|
| 91 |
+
transcription = client.audio.transcriptions.create(
|
| 92 |
+
file=("input.wav", audio_value, "audio/wav"),
|
| 93 |
+
model="whisper-large-v3",
|
| 94 |
+
response_format="text"
|
| 95 |
+
)
|
| 96 |
+
user_text = transcription
|
| 97 |
+
|
| 98 |
+
# B) Kullanıcı Mesajını Ekle
|
| 99 |
+
st.session_state.chat_history.append({"role": "user", "content": user_text})
|
| 100 |
+
|
| 101 |
+
# C) LLM'den Cevap Al
|
| 102 |
+
with st.spinner("Zara Düşünüyor..."):
|
| 103 |
+
ai_response = get_response_from_llm(st.session_state.chat_history, st.session_state.resume_text)
|
| 104 |
+
|
| 105 |
+
st.session_state.chat_history.append({"role": "assistant", "content": ai_response})
|
| 106 |
+
|
| 107 |
+
# D) Cevabı Sese Çevir ve Çal
|
| 108 |
+
audio_file = text_to_speech(ai_response)
|
| 109 |
+
st.audio(audio_file, format="audio/mp3", autoplay=True)
|
| 110 |
+
|
| 111 |
+
st.rerun()
|