Spaces:
Runtime error
Runtime error
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/Bart-Finetuned-conversational-summarization", | |
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() |