Spaces:
Sleeping
Sleeping
File size: 3,474 Bytes
3121096 31518a7 3121096 5be2b7e 3121096 5be2b7e |
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 75 76 77 |
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("<h1>🚀 Mistral 7B vs LLama3 8B 🦙</h1>")
gr.Markdown("<h3> 🕹️ Compare the performance and responses of two powerful models, Mistral 7B and LLama3 8B instruct. Type your questions or prompts below and see how each model responds to the same input 👾 </h3>")
with gr.Row():
input_text = gr.Textbox(label="Enter your prompt here:", placeholder="Type something...", lines=2)
submit_button = gr.Button("Submit")
output_llama = gr.Textbox(label="Llama 3 8B 👾", placeholder="", lines=10, interactive=False)
output_mistral = gr.Textbox(label="Mistral 7B 🌠", placeholder="", lines=10, interactive=False)
submit_button.click(fn=chat_with_models, inputs=input_text, outputs=[output_llama, output_mistral])
if __name__ == "__main__":
demo.launch()
|