import os import gradio as gr import requests import json hf_token = os.getenv("HF_TOKEN") def query_llama(text): API_URL = "https://api-inference.huggingface.co/models/meta-llama/Meta-Llama-3-8B-Instruct" headers = {"Authorization": f"Bearer {hf_token}"} # Use the token from environment variable payload = {"inputs": text} try: response = requests.post(API_URL, headers=headers, json=payload) response.raise_for_status() # Raises an exception for 4XX/5XX errors data = response.json() print("Llama Response Data:", json.dumps(data, indent=2)) # Print formatted JSON response # Check if the response is a list containing a dictionary if isinstance(data, list) and len(data) > 0 and isinstance(data[0], dict): return data[0].get('generated_text', "Error: 'generated_text' key not found in response") else: return "Error: Invalid response format" except requests.RequestException as e: print("HTTP Error:", e) return "Error: HTTP request failed" except KeyError: print("Key Error: 'generated_text' not in response") print("Response content:", response.text) return "Error: 'generated_text' key not found in response" def query_mistral(text): API_URL = "https://api-inference.huggingface.co/models/mistralai/Mistral-7B-Instruct-v0.2" headers = {"Authorization": f"Bearer {hf_token}"} # Use the token from environment variable payload = {"inputs": text} try: response = requests.post(API_URL, headers=headers, json=payload) response.raise_for_status() # Raises an exception for 4XX/5XX errors data = response.json() print("Mistral Response Data:", json.dumps(data, indent=2)) # Print formatted JSON response # Check if the response is a list containing a dictionary if isinstance(data, list) and len(data) > 0 and isinstance(data[0], dict): return data[0].get('generated_text', "Error: 'generated_text' key not found in response") else: return "Error: Invalid response format" except requests.RequestException as e: print("HTTP Error:", e) return "Error: HTTP request failed" except KeyError: print("Key Error: 'generated_text' not in response") print("Response content:", response.text) return "Error: 'generated_text' key not found in response" def chat_with_models(text): llama_response = query_llama(text) mistral_response = query_mistral(text) return llama_response, mistral_response with gr.Blocks() as demo: gr.Markdown("