File size: 2,100 Bytes
44aba0e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import requests
import json
import os

API_TOKEN = os.getenv("HF_API_TOKEN")
TRANSCRIBE_API_URL = "https://api-inference.huggingface.co/models/facebook/wav2vec2-base-960h"
LLM_API_URL = "https://api-inference.huggingface.co/models/mistralai/Mistral-7B-v0.1"

def transcribe_audio(audio_file):
    """Transcribe audio file to text."""
    headers = {"Authorization": f"Bearer {API_TOKEN}"}
    with open(audio_file, "rb") as f:
        data = f.read()
    response = requests.post(TRANSCRIBE_API_URL, headers=headers, data=data)
    transcription = json.loads(response.content.decode("utf-8")).get("text", "Transcription not available")
    return transcription

def get_answer(context, question):
    """Get an answer from the LLM based on the context and question."""
    prompt = f"Context: {context}\nQuestion: {question}\nAnswer:"
    headers = {"Authorization": f"Bearer {API_TOKEN}"}
    response = requests.post(LLM_API_URL, headers=headers, json={"inputs": prompt})
    answer = json.loads(response.content.decode("utf-8"))[0].get("generated_text", "Answer not available")
    return answer

def transcribe_and_answer(audio_file, question):
    """Process the audio file for transcription and use the result to get an answer to a question."""
    transcription = transcribe_audio(audio_file)
    answer = get_answer(transcription, question)
    return transcription, answer

# Create the Gradio app
with gr.Blocks() as app:
    gr.Markdown("### Audio to Text and Q&A Chatbot")
    with gr.Row():
        # Corrected 'type' parameter value to 'filepath'
        audio_input = gr.Audio(type="filepath", label="Upload your audio question")
        question_input = gr.Textbox(label="Type your question here")
    answer_button = gr.Button("Get Answer")
    with gr.Row():
        transcription_output = gr.Textbox(label="Transcription")
        answer_output = gr.Textbox(label="Answer")
    
    answer_button.click(transcribe_and_answer, inputs=[audio_input, question_input], outputs=[transcription_output, answer_output])


if __name__ == "__main__":
    app.launch()