import json import gradio as gr from huggingface_hub import InferenceClient from huggingface_hub.utils._errors import HfHubHTTPError import os from huggingface_hub.utils._errors import HfHubHTTPError print("HfHubHTTPError is available.") # Load the specific questions and answers from the JSON file with open('promptlist.json', 'r') as file: prompt_data = json.load(file) # Use the provided access token HF_TOKEn = os.getenv('HF_TOKEN') print(os.getenv('HF_TOKEN')) print({HF_TOKEn}) print(f"Your Hugging Face token is: {HF_TOKEn}") # Initialize the Hugging Face Inference Client with the access token client = InferenceClient( # "meta-llama/Llama-3.2-3B-Instruct", token=HF_TOKEn ) def chat_mem(message, chat_history): chat_history_role = [{"role": "system", "content": "You are a helpful assistant."}] if chat_history: for user_msg, assistant_msg in chat_history: chat_history_role.append({"role": "user", "content": user_msg}) chat_history_role.append({"role": "assistant", "content": assistant_msg}) chat_history_role.append({"role": "user", "content": message}) # Check for specific questions from prompt.json specific_question_found = False for item in prompt_data: if message.strip().lower() in [q.strip().lower() for q in item["prompt"]]: assistant_reply = item["completion"] specific_question_found = True break if not specific_question_found: try: chat_completion = client.chat_completion( messages=chat_history_role, max_tokens=500, ) assistant_reply = chat_completion.choices[0].message.content except HfHubHTTPError as e: if e.response.status_code == 429: # Rate limit error assistant_reply = "Rate limit reached. Please try again later." else: assistant_reply = "An error occurred. Please try again." chat_history.append((message, assistant_reply)) return "", chat_history, chat_history # Return the message, state, and chatbot history with gr.Blocks() as demo: with gr.Column(): gr.HTML("""
FAME AI ASSISTANT