Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# -*- coding: utf-8 -*-
|
2 |
+
"""OpenAI Whisper from Hugging Face Transformers with Microsoft PHI 3 Integration"""
|
3 |
+
|
4 |
+
import gradio as gr
|
5 |
+
from transformers import pipeline
|
6 |
+
import torch
|
7 |
+
from huggingface_hub import InferenceClient
|
8 |
+
import os
|
9 |
+
|
10 |
+
# Initialize the InferenceClient for PHI 3
|
11 |
+
client = InferenceClient(
|
12 |
+
"microsoft/Phi-3.5-mini-instruct", # Update this to the correct model name for PHI 3
|
13 |
+
token=os.getenv("HF_API_TOKEN", "") # You can configure this API token through the Hugging Face Secrets
|
14 |
+
)
|
15 |
+
|
16 |
+
# Check if a GPU is available and use it if possible
|
17 |
+
device = 'cuda' if torch.cuda.is_available() else 'cpu'
|
18 |
+
|
19 |
+
# Initialize the Whisper pipeline
|
20 |
+
whisper = pipeline('automatic-speech-recognition', model='openai/whisper-tiny', device=0 if device == 'cuda' else -1)
|
21 |
+
|
22 |
+
# Instructions (can be set through Hugging Face Secrets or hardcoded)
|
23 |
+
instructions = os.getenv("INST", "Your default instructions here.")
|
24 |
+
|
25 |
+
def query_phi(prompt):
|
26 |
+
response = "" # Initialize an empty string to store the response
|
27 |
+
for message in client.chat_completion(
|
28 |
+
messages=[{"role": "user", "content": f"{instructions}\n{prompt}"}],
|
29 |
+
max_tokens=500,
|
30 |
+
stream=True,
|
31 |
+
):
|
32 |
+
response += message.choices[0].delta.content # Append each message to the response
|
33 |
+
return response # Return the accumulated response after the loop
|
34 |
+
|
35 |
+
def transcribe_and_query(audio):
|
36 |
+
# Transcribe the audio file
|
37 |
+
transcription = whisper(audio)["text"]
|
38 |
+
transcription = "Prompt : " + transcription
|
39 |
+
# Query Microsoft PHI 3 with the transcribed text
|
40 |
+
phi_response = query_phi(transcription)
|
41 |
+
|
42 |
+
return transcription, phi_response
|
43 |
+
|
44 |
+
# Create Gradio interface
|
45 |
+
iface = gr.Interface(
|
46 |
+
fn=transcribe_and_query,
|
47 |
+
inputs=gr.Audio(type="filepath"),
|
48 |
+
outputs=["text", "text"],
|
49 |
+
title="Scam Call detector with BEEP",
|
50 |
+
description="Upload your recorded call to see if it is a scam or not. /n Stay Safe, Stay Secure."
|
51 |
+
)
|
52 |
+
|
53 |
+
# Launch the interface
|
54 |
+
iface.launch(share=True) # share=True is optional, it provides a public link
|