File size: 2,170 Bytes
168c0a1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a5e699c
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import os
import streamlit as st
import whisper
from groq import Groq

# Initialize Groq client
client = Groq(api_key="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")

# Load Whisper model
model = whisper.load_model("base")

def transcribe_video(video_path):
    """Transcribes audio from video using Whisper."""
    result = model.transcribe(video_path)
    return result["text"]

def summarize_text(text):
    """Summarizes text using Groq API."""
    chat_completion = client.chat.completions.create(
        messages=[{"role": "user", "content": f"Summarize the following text: {text}"}],
        model="llama-3.3-70b-versatile",
    )
    return chat_completion.choices[0].message.content

def answer_question(text, question):
    """Answers user questions based on video content."""
    chat_completion = client.chat.completions.create(
        messages=[
            {"role": "system", "content": "You are an assistant who answers questions based on the given text."},
            {"role": "user", "content": f"Context: {text}\nQuestion: {question}"},
        ],
        model="llama-3.3-70b-versatile",
    )
    return chat_completion.choices[0].message.content

# Streamlit UI
st.title("Video QA and Summarization using Whisper & Groq")

uploaded_file = st.file_uploader("Upload a video", type=["mp4", "mov", "avi"])

if uploaded_file is not None:
    video_path = f"temp_{uploaded_file.name}"
    with open(video_path, "wb") as f:
        f.write(uploaded_file.getbuffer())
    
    st.video(video_path)
    st.write("Transcribing...")
    transcript = transcribe_video(video_path)
    st.text_area("Transcript", transcript, height=200)
    
    st.write("Summarizing...")
    summary = summarize_text(transcript)
    st.text_area("Summary", summary, height=150)
    
    question = st.text_input("Ask a question about the video")
    if question:
        answer = answer_question(transcript, question)
        st.write("Answer:", answer)

# Deploy on Hugging Face: Save this script as `app.py` and push it to a Hugging Face Space with Streamlit.
        # Footer
st.markdown("---")
st.write("Built MERAJ HUSSAIN ❤️ ")