Spaces:
Sleeping
Sleeping
Angelawork
commited on
Commit
·
51e6078
1
Parent(s):
56950e0
utilities and global variables
Browse files- globals.py +31 -0
- utility.py +33 -0
globals.py
ADDED
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import torch
|
3 |
+
HF_TOKEN = os.getenv('HF_TOKEN')
|
4 |
+
|
5 |
+
"""T5"""
|
6 |
+
T5_FILE_NAME = "model.safetensors"
|
7 |
+
simplet5_base_URL="angel1987/simplet5_metaphor_dev1"
|
8 |
+
simplet5_large_URL="angel1987/simplet5_metaphor_dev2"
|
9 |
+
|
10 |
+
"""models"""
|
11 |
+
gemma_2b_URL = "google/gemma-2b-it"
|
12 |
+
Qwen_URL="Qwen/Qwen1.5-0.5B-Chat"
|
13 |
+
falcon_7b_URL = "tiiuae/falcon-7b-instruct"
|
14 |
+
falcon_1b_URL = "tiiuae/falcon-rw-1b"
|
15 |
+
|
16 |
+
if torch.cuda.is_available():
|
17 |
+
device_map = "auto" #use GPU if available
|
18 |
+
else:
|
19 |
+
device_map = "cpu"
|
20 |
+
print("No GPU found, using CPU.")
|
21 |
+
|
22 |
+
TITLE = "LiteraLingo_TopK_dev"
|
23 |
+
DESCRIPTION = "Figurative sentences to literal meanings."
|
24 |
+
|
25 |
+
EXAMPLE = [["gemma", "She has a heart of gold",256],
|
26 |
+
["gemma", "Time flies when you're having fun",128],
|
27 |
+
["falcon_api", "The sky is the limit",200]
|
28 |
+
]
|
29 |
+
gemma_PREFIX="Explain literal meaning of sentence: {fig}, Literal meaning:"
|
30 |
+
falcon_PREFIX="Paraphrase the following sentences from figurative to literal meaning: {fig}"
|
31 |
+
simplet5_PREFIX="Give the literal meaning of the sentence: {fig}"
|
utility.py
ADDED
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import urllib.request
|
3 |
+
import gradio as gr
|
4 |
+
from transformers import T5Tokenizer, T5ForConditionalGeneration
|
5 |
+
import huggingface_hub
|
6 |
+
import re
|
7 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
8 |
+
import torch
|
9 |
+
import time
|
10 |
+
import transformers
|
11 |
+
import requests
|
12 |
+
import globals
|
13 |
+
|
14 |
+
def fetch_model(url, filename):
|
15 |
+
if not os.path.isfile(filename):
|
16 |
+
urllib.request.urlretrieve(url, filename)
|
17 |
+
print("File downloaded successfully.")
|
18 |
+
else:
|
19 |
+
print("File already exists.")
|
20 |
+
|
21 |
+
def api_query(API_URL, headers, payload):
|
22 |
+
response = requests.post(API_URL, headers=headers, json=payload)
|
23 |
+
return response.json()
|
24 |
+
|
25 |
+
def post_process(model_output,input):
|
26 |
+
start_pos = model_output.find(input)
|
27 |
+
if start_pos != -1:
|
28 |
+
answer = model_output[start_pos + len(input):].strip()
|
29 |
+
else:
|
30 |
+
answer = model_output
|
31 |
+
print("'Literal meaning:' not found in the text.")
|
32 |
+
answer.replace("\n", "")
|
33 |
+
return answer
|