Shreyas94 commited on
Commit
e26bc82
·
verified ·
1 Parent(s): d5dae39

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +81 -63
app.py CHANGED
@@ -1,63 +1,81 @@
1
- import gradio as gr
2
- from huggingface_hub import InferenceClient
3
-
4
- """
5
- For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
6
- """
7
- client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
8
-
9
-
10
- def respond(
11
- message,
12
- history: list[tuple[str, str]],
13
- system_message,
14
- max_tokens,
15
- temperature,
16
- top_p,
17
- ):
18
- messages = [{"role": "system", "content": system_message}]
19
-
20
- for val in history:
21
- if val[0]:
22
- messages.append({"role": "user", "content": val[0]})
23
- if val[1]:
24
- messages.append({"role": "assistant", "content": val[1]})
25
-
26
- messages.append({"role": "user", "content": message})
27
-
28
- response = ""
29
-
30
- for message in client.chat_completion(
31
- messages,
32
- max_tokens=max_tokens,
33
- stream=True,
34
- temperature=temperature,
35
- top_p=top_p,
36
- ):
37
- token = message.choices[0].delta.content
38
-
39
- response += token
40
- yield response
41
-
42
- """
43
- For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
44
- """
45
- demo = gr.ChatInterface(
46
- respond,
47
- additional_inputs=[
48
- gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
49
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
50
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
51
- gr.Slider(
52
- minimum=0.1,
53
- maximum=1.0,
54
- value=0.95,
55
- step=0.05,
56
- label="Top-p (nucleus sampling)",
57
- ),
58
- ],
59
- )
60
-
61
-
62
- if __name__ == "__main__":
63
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import wna_googlenews as wna
3
+ import pandas as pd
4
+ from transformers import pipeline
5
+
6
+ st.set_page_config(layout="wide")
7
+
8
+ st.title("WNA Google News App")
9
+ st.subheader("Search for News and classify the headlines with sentiment analysis")
10
+
11
+ query = st.text_input("Enter Query")
12
+
13
+ models = [
14
+ "j-hartmann/emotion-english-distilroberta-base",
15
+ "SamLowe/roberta-base-go_emotions"
16
+ # "distilbert/distilbert-base-uncased-finetuned-sst-2-english"
17
+ ]
18
+
19
+ settings = {
20
+ "langregion": "en/US",
21
+ "period": "1d",
22
+ "model": models[0],
23
+ "number_of_pages": 5
24
+ }
25
+
26
+
27
+ with st.sidebar:
28
+ st.title("Settings")
29
+ # add language and country parameters
30
+ st.header("Language and Country")
31
+
32
+ settings["langregion"] = st.selectbox("Select Language", ["en/US", "fr/FR"])
33
+ # input field for number of pages
34
+ st.header("Number of Pages")
35
+ settings["number_of_pages"] = st.number_input("Enter Number of Pages", min_value=1, max_value=10)
36
+
37
+ settings["region"] = settings["langregion"].split("/")[0]
38
+ settings["lang"] = settings["langregion"].split("/")[1]
39
+
40
+ # add period parameter
41
+ st.header("Period")
42
+ settings["period"] = st.selectbox("Select Period", ["1d", "7d", "30d"])
43
+ # Add models parameters
44
+ st.header("Models")
45
+ settings["model"] = st.selectbox("Select Model", models)
46
+
47
+
48
+ if st.button("Search"):
49
+ classifier = pipeline(task="text-classification", model=settings["model"], top_k=None)
50
+ # display a loading progress
51
+ with st.spinner("Loading last news ..."):
52
+ allnews = wna.get_news(settings, query)
53
+ st.dataframe(allnews)
54
+ with st.spinner("Processing received news ..."):
55
+ df = pd.DataFrame(columns=["sentence", "date","best","second"])
56
+ # loop on each sentence and call classifier
57
+ for curnews in allnews:
58
+ #st.write(curnews)
59
+ cur_sentence = curnews["title"]
60
+ cur_date = curnews["date"]
61
+ model_outputs = classifier(cur_sentence)
62
+ cur_result = model_outputs[0]
63
+ #st.write(cur_result)
64
+ # get label 1
65
+ label = cur_result[0]['label']
66
+ score = cur_result[0]['score']
67
+ percentage = round(score * 100, 2)
68
+ str1 = label + " (" + str(percentage) + ")%"
69
+ # get label 2
70
+ label = cur_result[1]['label']
71
+ score = cur_result[1]['score']
72
+ percentage = round(score * 100, 2)
73
+ str2 = label + " (" + str(percentage) + ")%"
74
+ # insert cur_sentence and cur_result into dataframe
75
+ df.loc[len(df.index)] = [cur_sentence, cur_date, str1, str2]
76
+
77
+ # write info on the output
78
+ st.write("Number of sentences:", len(df))
79
+ st.write("Language:", settings["lang"], "Country:", settings["region"])
80
+
81
+ st.dataframe(df)