import os
import requests
import gradio as gr

api_token = os.environ.get("TOKEN")
API_URL = "https://api-inference.huggingface.co/models/meta-llama/Meta-Llama-3-8B-Instruct"
headers = {"Authorization": f"Bearer {api_token}"}



def query(payload):
    response = requests.post(API_URL, headers=headers, json=payload)
    return response.json()

def analyze_sentiment(text):
    output = query({
        "inputs": f'''<|begin_of_text|>
<|start_header_id|>system<|end_header_id|>
You are a feeling analyser and you'll say only "positive1" if I'm feeling positive and "negative1" if I'm feeling sad.
<|eot_id|>
<|start_header_id|>user<|end_header_id|>
{text}
<|eot_id|>
<|start_header_id|>assistant<|end_header_id|>

'''
    })



    if isinstance(output, list) and len(output) > 0:
        response = output[0].get('generated_text', '').strip().lower()

        positive_count = response.count('positive1')
        negative_count = response.count('negative1')
    if positive_count >= 2:
        return 'positive'
    elif negative_count >= 2:
        return 'negative'
    else:
        return f"oups I don't know"


demo = gr.Interface(
    fn=analyze_sentiment,
    inputs="text",
    outputs="text"
)

demo.launch()