safiaa02 commited on
Commit
93dd694
·
verified ·
1 Parent(s): 17b1cba

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -35
app.py CHANGED
@@ -1,51 +1,49 @@
1
  import os
2
  import gradio as gr
3
- import torch
4
- from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
5
- from huggingface_hub import login
6
-
7
- # Authenticate with Hugging Face using your secret token
8
- login(token=os.getenv("HUGGINGFACE_TOKEN"))
9
-
10
- MODEL_NAME = "mistralai/Mistral-7B-Instruct-v0.1"
11
-
12
- # Load model and tokenizer using token
13
- tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME, use_auth_token=True)
14
- model = AutoModelForCausalLM.from_pretrained(
15
- MODEL_NAME,
16
- torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
17
- device_map="auto", # Automatically chooses CPU or GPU
18
- use_auth_token=True
19
- )
20
-
21
- # Create a pipeline
22
- generator = pipeline("text-generation", model=model, tokenizer=tokenizer)
23
-
24
- # Define emotion annotation pipeline
25
  def emotion_annotator(text):
26
- # Step 1: Emotion candidates
27
- prompt1 = f"""List all possible emotions the person might be feeling based on this sentence:
28
- "{text}"
 
29
  Emotions:"""
30
- emotions_output = generator(prompt1, max_new_tokens=100)[0]["generated_text"]
31
- emotion_list = emotions_output.split("Emotions:")[-1].strip()
 
 
32
 
33
- # Step 2: Disambiguation
34
- prompt2 = f"""Now from this list of emotions: {emotion_list}, pick the most likely emotion the person is feeling and explain why. Text: "{text}" """
35
- disambiguation = generator(prompt2, max_new_tokens=150)[0]["generated_text"]
 
36
 
37
- return emotion_list, disambiguation
38
 
39
- # Gradio interface
40
  demo = gr.Interface(
41
  fn=emotion_annotator,
42
- inputs=gr.Textbox(lines=2, placeholder="Type a journal entry or emotional sentence..."),
43
  outputs=[
44
  gr.Textbox(label="Candidate Emotions"),
45
  gr.Textbox(label="Most Likely Emotion + Explanation")
46
  ],
47
- title="🧠 Emotion Annotator AI",
48
- description="Disambiguate mixed feelings in text using Mistral 7B Instruct model via Hugging Face token authentication"
49
  )
50
 
51
  demo.launch()
 
1
  import os
2
  import gradio as gr
3
+ import requests
4
+
5
+ # Set up Hugging Face Inference API
6
+ API_URL = "https://api-inference.huggingface.co/models/mistralai/Mistral-7B-Instruct-v0.1"
7
+ headers = {"Authorization": f"Bearer {os.getenv('HUGGINGFACE_TOKEN')}"}
8
+
9
+ def call_model(prompt):
10
+ payload = {"inputs": prompt}
11
+ response = requests.post(API_URL, headers=headers, json=payload)
12
+
13
+ try:
14
+ return response.json()[0]["generated_text"]
15
+ except (KeyError, IndexError, TypeError):
16
+ return "⚠️ Error: Could not generate a valid response. Check API status or token."
17
+
18
+ # Emotion disambiguation pipeline
 
 
 
 
 
 
19
  def emotion_annotator(text):
20
+ # Step 1: List candidate emotions
21
+ prompt1 = f"""You are an expert in human emotions. Based on the sentence below, list all possible emotions the person might be feeling.
22
+
23
+ Sentence: "{text}"
24
  Emotions:"""
25
+ candidates = call_model(prompt1)
26
+
27
+ # Step 2: Choose most likely one
28
+ prompt2 = f"""You are an emotion disambiguation expert. Given the sentence "{text}" and the following candidate emotions: {candidates}, pick the most likely one and explain why.
29
 
30
+ Format:
31
+ Most likely emotion: <emotion>
32
+ Reason: <why you chose it>"""
33
+ final = call_model(prompt2)
34
 
35
+ return candidates.strip(), final.strip()
36
 
37
+ # Gradio UI
38
  demo = gr.Interface(
39
  fn=emotion_annotator,
40
+ inputs=gr.Textbox(lines=2, placeholder="e.g., I’m proud but I feel like I let them down."),
41
  outputs=[
42
  gr.Textbox(label="Candidate Emotions"),
43
  gr.Textbox(label="Most Likely Emotion + Explanation")
44
  ],
45
+ title="🧠 Emotion Annotator AI (via Hugging Face Inference API)",
46
+ description="Uses Mistral 7B Instruct on Hugging Face to analyze and disambiguate mixed emotions from text input. No model download needed!"
47
  )
48
 
49
  demo.launch()