File size: 1,728 Bytes
6b8d83a
a83f7f5
3cd3eca
 
62670d0
6b8d83a
62670d0
cf16c11
8ef187d
3cd3eca
62670d0
3cd3eca
e5a6da2
 
3cd3eca
 
 
 
5cb2533
3cd3eca
c18f4c1
3cd3eca
8ef187d
bcb5a2e
 
 
 
 
 
3cd3eca
bcb5a2e
9613838
c18f4c1
cf16c11
b86c107
cf16c11
c18f4c1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f74bb47
c18f4c1
 
6b8d83a
62670d0
cf16c11
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
import gradio as gr
from transformers import pipeline
import requests
import json
import os

def speechToText(file):

    api_key = os.getenv("veni18sttts")  
    API_URL = "https://api-inference.huggingface.co/models/openai/whisper-large-v3-turbo"
    headers = {"Authorization": f"Bearer {api_key}"}

    def query(file):
        with open(file, "rb") as f:
            data = f.read()
        response = requests.post(API_URL, headers=headers, data=data)
        return response.json()

    my_text = query(file)

    #sentences = my_text["text"].split(".")

    
    def translate(sentences):
        translation = pipeline("translation", model="Helsinki-NLP/opus-mt-en-hu")
    
        text_translated=[]
        for text in sentences:
            text_translated.append(translation(text))

        return text_translated
    
    #text_translate = translate(sentences)

    #combined_text = ' '.join([item['translation_text'] for sublist in text_translated for item in sublist])

    def summa(my_text):
        pipe = pipeline("summarization", model="facebook/bart-large-cnn")

        max = 1024 
        if len(my_text) > max:    
            combined_text_parts = [my_text[i:i + max] for i in range(0, len(my_text), max)]
        else:
            combined_text_parts = [my_text]

        summaries = []
        for part in combined_text_parts:
            summary = pipe(part, max_length=180, min_length=140, do_sample=False)
            summaries.append(summary[0]['summary_text'])


        full_summary = ' '.join(summaries)


        return full_summary
    
    full_summaries = summa(my_text["text"])

    return full_summaries

demo = gr.Interface(fn=speechToText, inputs="file", outputs="text")
demo.launch()