Spaces:
Runtime error
Runtime error
imran11439
commited on
Commit
·
be4fd28
1
Parent(s):
4682823
Add application file
Browse files- app.py +101 -0
- requirements.txt +4 -0
app.py
ADDED
@@ -0,0 +1,101 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
3 |
+
import torch
|
4 |
+
import re
|
5 |
+
from gtts import gTTS
|
6 |
+
import os
|
7 |
+
import logging
|
8 |
+
|
9 |
+
# Set up logging
|
10 |
+
logging.basicConfig(filename='app.log', level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
11 |
+
|
12 |
+
# Function to set up the model and tokenizer
|
13 |
+
def setup_model(model_name):
|
14 |
+
logging.info('Setting up model and tokenizer.')
|
15 |
+
model = AutoModelForCausalLM.from_pretrained(
|
16 |
+
model_name,
|
17 |
+
device_map="auto",
|
18 |
+
trust_remote_code=False,
|
19 |
+
revision="main"
|
20 |
+
)
|
21 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name, use_fast=True)
|
22 |
+
model.eval()
|
23 |
+
logging.info('Model and tokenizer setup completed.')
|
24 |
+
return model, tokenizer
|
25 |
+
|
26 |
+
# Function to generate a response from the model
|
27 |
+
def generate_response(model, tokenizer, prompt, max_new_tokens=140):
|
28 |
+
logging.info('Generating response for the prompt.')
|
29 |
+
inputs = tokenizer(prompt, return_tensors="pt")
|
30 |
+
outputs = model.generate(input_ids=inputs["input_ids"].to("cuda"), max_new_tokens=max_new_tokens)
|
31 |
+
response = tokenizer.batch_decode(outputs)[0]
|
32 |
+
|
33 |
+
# Extract only the response part (assuming everything after the last newline belongs to the response)
|
34 |
+
response_parts = response.split("\n")
|
35 |
+
logging.info('Response generated.')
|
36 |
+
return response_parts[-1] # Return the last element (response)
|
37 |
+
|
38 |
+
# Function to remove various tags using regular expressions
|
39 |
+
def remove_tags(text):
|
40 |
+
logging.info('Removing tags from the text.')
|
41 |
+
# Combine multiple tag removal patterns for broader coverage
|
42 |
+
tag_regex = r"<[^>]*>" # Standard HTML tags
|
43 |
+
custom_tag_regex = r"<.*?>|\[.*?\]|{\s*?\(.*?\)\s*}" # Custom, non-standard tags (may need adjustments)
|
44 |
+
all_tags_regex = f"{tag_regex}|{custom_tag_regex}" # Combine patterns
|
45 |
+
cleaned_text = re.sub(all_tags_regex, "", text)
|
46 |
+
logging.info('Tags removed.')
|
47 |
+
return cleaned_text
|
48 |
+
|
49 |
+
# Function to generate the audio file
|
50 |
+
def text_to_speech(text, filename="response.mp3"):
|
51 |
+
logging.info('Generating speech audio file.')
|
52 |
+
tts = gTTS(text)
|
53 |
+
tts.save(filename)
|
54 |
+
logging.info('Speech audio file saved.')
|
55 |
+
return filename
|
56 |
+
|
57 |
+
# Main function for the Gradio app
|
58 |
+
def main(comment):
|
59 |
+
logging.info('Main function triggered.')
|
60 |
+
instructions_string = (
|
61 |
+
"virtual marketer assistant, communicates in business, focused on services, "
|
62 |
+
"escalating to technical depth upon request. It reacts to feedback aptly and ends responses "
|
63 |
+
"with its signature Mr.jon will tailor the length of its responses to match the individual's comment, "
|
64 |
+
"providing concise acknowledgments to brief expressions of gratitude or feedback, thus keeping the interaction natural and supportive.\n"
|
65 |
+
)
|
66 |
+
|
67 |
+
model_name = "TheBloke/Mistral-7B-Instruct-v0.2-GPTQ"
|
68 |
+
try:
|
69 |
+
model, tokenizer = setup_model(model_name)
|
70 |
+
|
71 |
+
if comment:
|
72 |
+
prompt_template = lambda comment: f"[INST] {instructions_string} \n{comment} \n[/INST]"
|
73 |
+
prompt = prompt_template(comment)
|
74 |
+
|
75 |
+
response = generate_response(model, tokenizer, prompt)
|
76 |
+
# Apply tag removal before displaying the response
|
77 |
+
response_without_tags = remove_tags(response)
|
78 |
+
# Remove the "[/INST]" string at the end (assuming it's always present)
|
79 |
+
response_without_inst = response_without_tags.rstrip("[/INST]")
|
80 |
+
|
81 |
+
# Generate and return the response and the audio file
|
82 |
+
audio_file = text_to_speech(response_without_inst)
|
83 |
+
logging.info('Response and audio file generated.')
|
84 |
+
return response_without_inst, audio_file
|
85 |
+
else:
|
86 |
+
logging.warning('No comment entered.')
|
87 |
+
return "Please enter a comment to generate a response.", None
|
88 |
+
except Exception as e:
|
89 |
+
logging.error(f'Error occurred: {str(e)}')
|
90 |
+
return "An error occurred. Please try again later.", None
|
91 |
+
|
92 |
+
iface = gr.Interface(
|
93 |
+
fn=main,
|
94 |
+
inputs=gr.Textbox(lines=2, placeholder="Enter a comment..."),
|
95 |
+
outputs=["text", "file"],
|
96 |
+
title="Virtual Marketer Assistant",
|
97 |
+
description="Enter a comment and get a response from the virtual marketer assistant. Download the response as an MP3 file."
|
98 |
+
)
|
99 |
+
|
100 |
+
if __name__ == "__main__":
|
101 |
+
iface.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
gradio
|
2 |
+
transformers
|
3 |
+
torch
|
4 |
+
gtts
|