-
Notifications
You must be signed in to change notification settings - Fork 0
/
newz.py
51 lines (41 loc) · 1.7 KB
/
newz.py
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
import streamlit as st
from sorter import get_news
from hugchat import hugchat
from hugchat.login import Login
from dotenv import dotenv_values
import json
class NewsSummarizer:
def __init__(self):
self.secrets = dotenv_values('.env')
self.hf_email = self.secrets['EMAIL']
self.hf_pass = self.secrets['PASSWORD']
self.chatbot = None
# Initialize the messages list in the session state
if 'messages' not in st.session_state:
st.session_state.messages = []
def generate_response(self, prompt_input):
try:
# Hugging Face Login
sign = Login(self.hf_email, self.hf_pass)
cookies = sign.login()
# Create ChatBot
self.chatbot = hugchat.ChatBot(cookies=cookies.get_dict())
response = self.chatbot.chat(prompt_input)
return response
except Exception as e:
st.error(f"Error occurred: {e}")
return "Sorry, I encountered an error. Please try again later."
def call_required_functions(self, required_actions):
if not self.run:
return
tool_output = []
for action in required_actions:
function_name = action["function"]["name"]
arguments = json.loads(action["function"]["arguments"])
if function_name == "get_news":
output = get_news(topic=arguments["topic"])
print(f"NEWS: {output}")
final_str = ""
for item in output:
final_str += "".join(item)
tool_output.append({"tool_call_id": action["id"], "output": final_str})