File size: 2,868 Bytes
c14a846
 
3dd1351
dedbfd2
 
c14a846
3dd1351
 
 
 
c14a846
3dd1351
 
ce91f42
dedbfd2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c14a846
3dd1351
7e4be94
f574d39
c14a846
7e4be94
 
 
3dd1351
7e4be94
c14a846
7e4be94
 
3dd1351
7e4be94
 
 
 
3dd1351
c14a846
3dd1351
 
 
c14a846
3dd1351
6aa1f96
c14a846
7e4be94
 
c14a846
7e4be94
 
 
 
 
 
55ad160
c14a846
 
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import streamlit as st
import os
import requests
import time


# Define Hugging Face API details
API_URL = "https://api-inference.huggingface.co/models/Huzaifa367/chat-summarizer"
API_TOKEN = os.getenv("AUTH_TOKEN")
HEADERS = {"Authorization": f"Bearer {API_TOKEN}"}

# Function to query Hugging Face API
def query_huggingface(payload):
    max_retries=3
    retries = 0
    while retries < max_retries:
        try:
            response = requests.post(API_URL, headers=HEADERS, json=payload)
            response.raise_for_status()  # Raise exception for non-2xx status codes
            return response.json()
        except requests.exceptions.RequestException as e:
            st.error(f"Error querying Hugging Face API: {e}")
            return {"summary_text": f"Error querying Hugging Face API: {e}"}
        except requests.exceptions.HTTPError as e:
            if response.status_code == 503:
                st.warning("Service temporarily unavailable. Retrying...")
                time.sleep(5)  # Wait for a few seconds before retrying
                retries += 1
            else:
                st.error(f"HTTP Error querying Hugging Face API: {e.response.status_code}")
                return {"summary_text": "Service Unavailable. Please try again later."}
        except Exception as e:
            st.error(f"An unexpected error occurred: {e}")
            return {"summary_text": "An unexpected error occurred. Please try again later."}

def main():
    st.set_page_config(layout="wide")
    st.title("Chat Summarizer")

    # Initialize a list to store chat messages
    chat_history = []

    # User input for chat message
    user_message = st.text_input("Provide a Chat/Long description to summarize")

    # Process user input and query Hugging Face API on button click
    if st.button("Send"):
        if user_message:
            # Add user message to chat history
            chat_history.append({"speaker": "User", "message": user_message})

            # Construct input text for summarization
            input_text = f"User: {user_message}"

            # Query Hugging Face API for summarization
            payload = {"inputs": input_text}
            response = query_huggingface(payload)

            # Extract summary text from the API response
            summary_text = response[0]["summary_text"] if isinstance(response, list) else response.get("summary_text", "")

            # Add summarization response to chat history
            chat_history.append({"speaker": "Bot", "message": summary_text})

    # Display chat history as a conversation
    for chat in chat_history:
        if chat["speaker"] == "User":
            st.text_input("User", chat["message"], disabled=True)
        elif chat["speaker"] == "Bot":
            st.text_area("Bot", chat["message"], disabled=True)

if __name__ == "__main__":
    main()