File size: 2,196 Bytes
37f26bb
f6b082a
 
37f26bb
 
 
744e9ee
37f26bb
ac59d27
 
37f26bb
ff8a217
 
 
37f26bb
f6b082a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
43feb78
37f26bb
 
 
 
 
 
43feb78
37f26bb
 
 
 
 
 
 
 
f6b082a
 
37f26bb
 
 
e54fb95
 
 
 
 
 
aa8ebd1
37f26bb
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
import gradio as gr
# from langchain.document_loaders import UnstructuredURLLoader
from langchain_community.document_loaders import WebBaseLoader
import os
from langchain import PromptTemplate, HuggingFaceHub, LLMChain
from gtts import gTTS
# from IPython.display import Audio

# from dotenv import load_dotenv 
# load_dotenv()  # to load all the env variables

api_key = os.getenv("HUGGINGFACE_API_TOKEN")

os.environ['HUGGINGFACEHUB_API_TOKEN']  = api_key

def get_url_text(url_link):
    try:
        loader = WebBaseLoader(url_link)
        loader.requests_per_second = 1
        docs = loader.aload()
        extracted_text = ""
        for page in docs:
            extracted_text += page.page_content
        return extracted_text
    except Exception as e:
        print(f"Error fetching or processing URL: {e}")
        return ""

def get_answer(text):
    template = """{text}"""
    prompt = PromptTemplate(template=template, input_variables=["text"])
    
    llm_chain = LLMChain(prompt=prompt,
                         llm=HuggingFaceHub(repo_id="Mr-Vicky-01/conversational_sumarization",
                                            model_kwargs={"max_length":100,
                                                          "max_new_tokens":100,
                                                          "do_sample": False}))
    answer = llm_chain.run(text)
    return answer

def text_to_speech(text, language='en'):
    tts = gTTS(text=text, lang=language)
    tts.save("output.mp3")
    return "output.mp3"

def summarize_and_convert_to_audio(url):
    text = get_url_text(url)
    summary = get_answer(text)
    audio_file = text_to_speech(summary)
    return audio_file

example = [
    ["https://en.wikipedia.org/wiki/Vijay_(actor)"],
    ["https://en.wikipedia.org/wiki/Sam_Altman"],
    ["https://timesofindia.indiatimes.com/india/air-india-sacks-pilot-found-drunk-after-operating-overseas-flight/articleshow/108829830.cms"]
]

iface = gr.Interface(fn=summarize_and_convert_to_audio, inputs="text", outputs="audio", title="Text Summarization & Audio Generation", description="Enter the URL of the article to summarize and convert to audio.", examples=example)
iface.launch()