Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from transformers import (
|
3 |
+
MarianMTModel, MarianTokenizer,
|
4 |
+
GPT2LMHeadModel, GPT2Tokenizer,
|
5 |
+
pipeline
|
6 |
+
)
|
7 |
+
st.title("Multi Chatbot")
|
8 |
+
models = {
|
9 |
+
"English to French": {
|
10 |
+
"name": "Helsinki-NLP/opus-mt-en-fr",
|
11 |
+
"description": "Translate English text to French."
|
12 |
+
},
|
13 |
+
"Sentiment Analysis": {
|
14 |
+
"name": "distilbert-base-uncased-finetuned-sst-2-english",
|
15 |
+
"description": "Analyze the sentiment of input text."
|
16 |
+
},
|
17 |
+
"Story Generator": {
|
18 |
+
"name": "distilgpt2",
|
19 |
+
"description": "Generate creative stories based on input."
|
20 |
+
}
|
21 |
+
}
|
22 |
+
st.sidebar.header("Choose a Model")
|
23 |
+
selected_model_key = st.sidebar.radio("Select a Model:", list(models.keys()))
|
24 |
+
model_name = models[selected_model_key]["name"]
|
25 |
+
model_description = models[selected_model_key]["description"]
|
26 |
+
st.sidebar.markdown(f"### Model Description\n{model_description}")
|
27 |
+
try:
|
28 |
+
if selected_model_key == "English to French":
|
29 |
+
st.write("Loading English to French model...")
|
30 |
+
tokenizer = MarianTokenizer.from_pretrained(model_name)
|
31 |
+
model = MarianMTModel.from_pretrained(model_name)
|
32 |
+
st.write("English to French model loaded successfully.")
|
33 |
+
elif selected_model_key == "Sentiment Analysis":
|
34 |
+
st.write("Loading Sentiment Analysis model...")
|
35 |
+
sentiment_analyzer = pipeline("sentiment-analysis", model=model_name)
|
36 |
+
st.write("Sentiment Analysis model loaded successfully.")
|
37 |
+
elif selected_model_key == "Story Generator":
|
38 |
+
st.write("Loading Story Generator model...")
|
39 |
+
tokenizer = GPT2Tokenizer.from_pretrained("distilgpt2")
|
40 |
+
model = GPT2LMHeadModel.from_pretrained("distilgpt2")
|
41 |
+
tokenizer.pad_token = tokenizer.eos_token
|
42 |
+
st.write("Story Generator model loaded successfully.")
|
43 |
+
except Exception as e:
|
44 |
+
st.error(f"Failed to load the model: {e}")
|
45 |
+
user_input = st.text_input("Enter your query:")
|
46 |
+
if user_input:
|
47 |
+
if selected_model_key == "English to French":
|
48 |
+
try:
|
49 |
+
inputs = tokenizer(user_input, return_tensors="pt", truncation=True, padding=True)
|
50 |
+
outputs = model.generate(inputs["input_ids"], max_length=150, num_return_sequences=1, no_repeat_ngram_size=2)
|
51 |
+
bot_response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
52 |
+
st.write(f"Translated Text: {bot_response}")
|
53 |
+
except Exception as e:
|
54 |
+
st.error(f"Error during translation: {e}")
|
55 |
+
elif selected_model_key == "Sentiment Analysis":
|
56 |
+
try:
|
57 |
+
result = sentiment_analyzer(user_input)[0]
|
58 |
+
st.write(f"Sentiment: {result['label']}")
|
59 |
+
st.write(f"Confidence: {result['score']:.2f}")
|
60 |
+
except Exception as e:
|
61 |
+
st.error(f"Error during sentiment analysis: {e}")
|
62 |
+
elif selected_model_key == "Story Generator":
|
63 |
+
try:
|
64 |
+
inputs = tokenizer(user_input, return_tensors="pt", truncation=True, padding=True)
|
65 |
+
outputs = model.generate(inputs["input_ids"], max_length=500, num_return_sequences=1, no_repeat_ngram_size=2, temperature=0.7)
|
66 |
+
bot_response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
67 |
+
st.write(f"Generated Story: {bot_response}")
|
68 |
+
except Exception as e:
|
69 |
+
st.error(f"Error during story generation: {e}")
|
70 |
+
|