File size: 4,008 Bytes
8d510d0
 
757dd46
8d510d0
 
 
757dd46
 
8d510d0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
757dd46
8d510d0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
757dd46
8d510d0
 
 
 
 
 
 
757dd46
8d510d0
 
 
757dd46
 
 
 
 
 
 
 
8d510d0
757dd46
8d510d0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
from transformers import pipeline
asr_pipe = pipeline("automatic-speech-recognition", model="Abdullah17/whisper-small-urdu")
transcript_pipe = pipeline("automatic-speech-recognition", model="ihanif/whisper-medium-urdu")
from difflib import SequenceMatcher
import json
import socket
import soundfile as sf
import gradio as gr
def get_local_ip():
    try:
        # Create a socket connection to a remote host (here, google.com)
        s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        s.connect(("8.8.8.8", 80))
        local_ip = s.getsockname()[0]
        s.close()
        return local_ip
    except Exception as e:
        print(f"Error getting local IP: {e}")
        return None



with open("tasks.json", "r",encoding="utf-8") as json_file:
    urdu_data = json.load(json_file)
# List of commands
# commands = [
#     "نمائندے ایجنٹ نمائندہ",
#     "  سم  ایکٹیویٹ ",
#     " سم  بلاک بند ",
#     "موبائل پیکیجز انٹرنیٹ پیکیج",
#     " چالان جمع چلان",
#     " گانا "
# ]
# replies = [
# 1,2,
# ]
# Function to find the most similar command
def find_most_similar_command(statement, command_list):
    best_match = None
    highest_similarity = 0
    i=0
    for sub_list in command_list:
     for command in sub_list:
        similarity = SequenceMatcher(None, statement, command).ratio()
        print(i,"similarity",similarity)
        if similarity > highest_similarity:
            highest_similarity = similarity
            best_match = command
            reply=i
     i+=1

    return best_match,reply

transcript_only=["1","3","4"]
match_and_save=["2"]
col_names={'1':"name",'3':"address",'4':"order"}
def send_data_to_db(menu_id,col_value,order_id):
    import requests
    col_name=col_names[menu_id]
    # API endpoint URL
    url = 'https://pizzahut.softinfix.tech/api/save_order?'+col_name+'='+col_value+"&order_id"+"="+order_id
    payload = {}
    headers = {}

    response = requests.request("GET", url, headers=headers, data=payload)

    # Print response
    print(response.status_code)
    print(response.text)

def transcribe_the_command(audio,menu_id,order_id):
      local_ip = get_local_ip()
      if local_ip:
         print(f"Local IP Address: {local_ip}")
      else:
         print("Local IP could not be determined.")

      sample_rate, audio_data = audio
      file_name = "recorded_audio.wav"
      sf.write(file_name, audio_data, sample_rate)
    # Convert stereo to mono by averaging the two channels
      print(menu_id)

      if menu_id in transcript_only:
        transcript = transcript_pipe(file_name)["text"]
        col_value=transcript
        send_data_to_db(menu_id,col_value,order_id)
        print("data uploaded successfully!")
      elif menu_id in match_and_save:
        transcript = asr_pipe(file_name)["text"]
        commands=urdu_data[menu_id]
        most_similar_command,reply = find_most_similar_command(transcript, commands)
        print(f"Given Statement: {transcript}")
        print(f"Most Similar Command: {most_similar_command}\n")
        print(reply)
        send_data_to_db(menu_id,reply,order_id)
      else:
        transcript = asr_pipe(file_name)["text"]
        commands=urdu_data[menu_id]
        print(commands)
        most_similar_command,reply = find_most_similar_command(transcript, commands)
        print(f"Given Statement: {transcript}")
        print(f"Most Similar Command: {most_similar_command}\n")
        print(reply)
        return reply
# get_text_from_voice("urdu.wav")


iface = gr.Interface(
    fn=transcribe_the_command,
    inputs=[gr.inputs.Audio(label="Recorded Audio",source="microphone"),gr.inputs.Textbox(label="menu_id"),gr.inputs.Textbox(label="order_id")],
    outputs="text",
    title="Whisper Small Urdu Command",
    description="Realtime demo for Urdu speech recognition using a fine-tuned Whisper small model and outputting the estimated command on the basis of speech transcript.",
)

iface.launch()