import streamlit as st import requests import os # Retrieve the API key from an environment variable or secret management service api_key = os.getenv("api_key") bearer = "Bearer "+api_key headers = {"Authorization": bearer} # API endpoint API_URL = "https://api-inference.huggingface.co/models/meta-llama/Llama-2-7b-chat-hf" # Function to query the API def query_model(payload): try: response = requests.post(API_URL, headers=headers, json=payload) response.raise_for_status() # Will raise an HTTPError if the HTTP request returned an unsuccessful status code return response.json() except requests.RequestException as e: st.write(f"An error occurred: {e}") return None # Initialize the conversation conversation = [] # Streamlit app st.title("Text Data API Query Chat UI") st.write("Type your text and click the button to send it to the API.") user_input = st.text_input("You: ") if st.button("Send"): if user_input: # Add user input to the conversation conversation.append({"user": user_input}) # Query the API payload = {"inputs": user_input} output = query_model(payload) if output: try: answer = output[0]['generated_text'][len(user_input):].strip() # Add API response to the conversation conversation.append({"bot": answer}) except KeyError: st.write("Unexpected structure in API response.") # Display the conversation for turn in conversation: if "user" in turn: st.write(f"You: {turn['user']}") elif "bot" in turn: st.write(f"Bot: {turn['bot']}") else: st.write("Please enter some text first.") # Usage Note st.write("---") st.write("**Note on Usage:**") st.write("This tool is intended for informational purposes only.")