Spaces:
Sleeping
Sleeping
Upload folder using huggingface_hub
Browse files- app.py +85 -0
- requirements.txt +3 -0
app.py
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import pipeline, Conversation
|
| 3 |
+
|
| 4 |
+
# Models
|
| 5 |
+
models = {
|
| 6 |
+
"text_generator": {"model": "EleutherAI/gpt-neo-1.3B", "task": "text-generation"},
|
| 7 |
+
"sentiment": {"model": "cardiffnlp/twitter-roberta-base-sentiment-latest", "task": "sentiment-analysis"},
|
| 8 |
+
"dialogue": {"model": "microsoft/DialoGPT-medium", "task": "conversational"},
|
| 9 |
+
"summarizer": {"model": "sshleifer/distilbart-cnn-12-6", "task": "summarization"},
|
| 10 |
+
"ner": {"model": "dbmdz/bert-large-uncased-finetuned-conll03-english", "task": "ner"},
|
| 11 |
+
}
|
| 12 |
+
|
| 13 |
+
# Initialize pipelines
|
| 14 |
+
pipelines = {}
|
| 15 |
+
for key, info in models.items():
|
| 16 |
+
pipelines[key] = pipeline(info["task"], model=info["model"])
|
| 17 |
+
|
| 18 |
+
def generate_text(prompt, max_length):
|
| 19 |
+
result = pipelines["text_generator"](prompt, max_length=int(max_length), num_return_sequences=1)
|
| 20 |
+
return result[0]['generated_text']
|
| 21 |
+
|
| 22 |
+
def analyze_sentiment(text):
|
| 23 |
+
result = pipelines["sentiment"](text)
|
| 24 |
+
return [{"label": res['label'], "score": f"{res['score']:.2f}"} for res in result]
|
| 25 |
+
|
| 26 |
+
def converse(message, state):
|
| 27 |
+
if state is None:
|
| 28 |
+
state = ""
|
| 29 |
+
result = pipelines["dialogue"](state + "<|endoftext|>" + message, pad_token_id=50256)
|
| 30 |
+
return result[0]['generated_text']
|
| 31 |
+
|
| 32 |
+
def summarize_text(text, max_length, min_length):
|
| 33 |
+
result = pipelines["summarizer"](text, max_length=int(max_length), min_length=int(min_length), do_sample=False)
|
| 34 |
+
return result[0]['summary_text']
|
| 35 |
+
|
| 36 |
+
def ner_analysis(text):
|
| 37 |
+
result = pipelines["ner"](text, aggregation_strategy="max")
|
| 38 |
+
formatted = [f"{res['entity_group']}: {res['word']} ({res['score']:.2f})" for res in result]
|
| 39 |
+
return "\n".join(formatted)
|
| 40 |
+
|
| 41 |
+
with gr.Blocks() as demo:
|
| 42 |
+
gr.Markdown("# AI Art Main Interface")
|
| 43 |
+
gr.Markdown("Use various AI models below")
|
| 44 |
+
|
| 45 |
+
with gr.Tab("Text Generator"):
|
| 46 |
+
gr.Markdown("## Text Generator (EleutherAI/gpt-neo-1.3B)")
|
| 47 |
+
gen_prompt = gr.Textbox(label="Prompt")
|
| 48 |
+
gen_len = gr.Slider(10, 200, 50, label="Max Length")
|
| 49 |
+
gen_btn = gr.Button()
|
| 50 |
+
gen_out = gr.Textbox(label="Output")
|
| 51 |
+
gen_btn.click(generate_text, [gen_prompt, gen_len], gen_out)
|
| 52 |
+
|
| 53 |
+
with gr.Tab("Sentiment Analysis"):
|
| 54 |
+
gr.Markdown("## Sentiment Analyzer (cardiffnlp/twitter-roberta-base-sentiment-latest)")
|
| 55 |
+
sent_text = gr.Textbox(label="Text")
|
| 56 |
+
sent_btn = gr.Button()
|
| 57 |
+
sent_out = gr.JSON(label="Result")
|
| 58 |
+
sent_btn.click(analyze_sentiment, sent_text, sent_out)
|
| 59 |
+
|
| 60 |
+
with gr.Tab("Dialogue Bot"):
|
| 61 |
+
gr.Markdown("## Dialogue Bot (microsoft/DialoGPT-medium)")
|
| 62 |
+
chat_state = gr.State()
|
| 63 |
+
chat_msg = gr.Textbox(label="Message")
|
| 64 |
+
chat_send = gr.Button()
|
| 65 |
+
chat_out = gr.Chatbot()
|
| 66 |
+
chat_send.click(converse, [chat_msg, chat_state], [chat_out, chat_state])
|
| 67 |
+
|
| 68 |
+
with gr.Tab("Text Summarizer"):
|
| 69 |
+
gr.Markdown("## Text Summarizer (sshleifer/distilbart-cnn-12-6)")
|
| 70 |
+
sum_text = gr.Textbox(lines=5, label="Text")
|
| 71 |
+
sum_max = gr.Slider(10, 200, 100, label="Max Length")
|
| 72 |
+
sum_min = gr.Slider(5, 50, 10, label="Min Length")
|
| 73 |
+
sum_btn = gr.Button()
|
| 74 |
+
sum_out = gr.Textbox(label="Summary")
|
| 75 |
+
sum_btn.click(summarize_text, [sum_text, sum_max, sum_min], sum_out)
|
| 76 |
+
|
| 77 |
+
with gr.Tab("Named Entity Recognition"):
|
| 78 |
+
gr.Markdown("## Named Entity Recognition (dbmdz/bert-large-uncased-finetuned-conll03-english)")
|
| 79 |
+
ner_text = gr.Textbox(lines=3, label="Text")
|
| 80 |
+
ner_btn = gr.Button()
|
| 81 |
+
ner_out = gr.Textbox(lines=5, label="Entities")
|
| 82 |
+
ner_btn.click(ner_analysis, ner_text, ner_out)
|
| 83 |
+
|
| 84 |
+
if __name__ == "__main__":
|
| 85 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
transformers
|
| 3 |
+
torch
|