conversational / app.py
psyneex's picture
Update app.py
1a1319e
# %%
from transformers import (
pipeline,
AutoModelForCausalLM,
GenerationConfig
)
import requests
import gradio as gr
from bs4 import BeautifulSoup
import torch
# %%
def search(subject):
#search the news for subject
base_url = "https://www.bing.com/search"
query = requests.utils.quote(subject)
count = 3
url = f"{base_url}?q={query}&count={count}"
response = requests.get(url)
if response.status_code == 200:
print("The request was successful!")
soup = BeautifulSoup(response.content, "html.parser")
results = soup.find_all("li")
titles = []
texts = []
for result in results:
title = result.find("h2")
text = result.find("p")
if title and text:
titles.append(title.text)
texts.append(text.text)
texts = "".join(texts)
#print(texts)
return texts
# %%
def answer(inp):
pipe = pipeline("conversational","HuggingFaceH4/zephyr-7b-beta")
chat = [
{"role": "system", "content": "As our News Analysis Assistant, your primary responsibility is to thoroughly read and interpret all the latest news articles that are given to you. Once you have comprehensively analyzed these pieces, your task is to condense their essential elements into succinct and precise summaries in English language and Persian. These summaries should accurately convey the most pertinent details while preserving the original meaning and intent of the source material. By doing so, you'll provide our users with timely and trustworthy reports on the topics they care about, no matter where those stories originate. Thank you for taking on this critical role, and we look forward to seeing how your expertise contributes to improving our platform's overall quality and effectiveness."},
{"role": "user", "content": search(inp)},
{"role": "assistant", "content": "here is a summary about" + inp}
]
return pipe(chat)
# %%
#output = answer(input("enter question"))
#print(output)
# %%
with gr.Blocks() as demo:
gr.Markdown("# News Analysis Assistant")
with gr.Row():
subject = gr.Textbox(label="Subject")
results = gr.Textbox()
with gr.Row():
submit = gr.Button("Submit")
submit.click(answer, inputs=subject, outputs=results)
demo.launch(share=True)