Spaces:
Sleeping
Sleeping
File size: 6,655 Bytes
15c1c8b c643c7d 15c1c8b 74f1ddf 15c1c8b 827e2e2 74f1ddf 827e2e2 15c1c8b 74f1ddf 15c1c8b 74f1ddf 15c1c8b 74f1ddf 15c1c8b |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 |
import streamlit as st
from streamlit_option_menu import option_menu
from transformers import pipeline
import torch
import time
import requests
import io
import os
from PIL import Image
# Load models
translator = pipeline("translation", model="Helsinki-NLP/opus-mt-dra-en")
# for summarizer api
SUMMARIZER_API_URL = "https://api.groq.com/openai/v1/chat/completions"
summarizer_headers = {"Authorization": f"Bearer {os.getenv('GROQ_API_TOKEN')}",
"Content-Type": "application/json"}
# for image api
IMAGE_API_URL = "https://api-inference.huggingface.co/models/ZB-Tech/Text-to-Image"
img_headers = {"Authorization": f"Bearer {os.getenv('HF_API_TOKEN')}"}
# Functions for each task
def translate_tamil_to_english(text):
time.sleep(2)
result = translator(text)
return result[0]['translation_text']
def summarize_english_text(paragraph):
time.sleep(2)
# Request payload
payload = {
"model": "mixtral-8x7b-32768",
"messages": [
{"role": "system", "content": "Create a summary of below paragraph in 30 words max"},
{"role": "user", "content": paragraph}
],
"max_tokens": 100 # number of words in the output.
}
# Send POST request to Groq API
response = requests.post(SUMMARIZER_API_URL, json=payload, headers=summarizer_headers)
# Check if the request was successful
if response.status_code == 200:
# Parse the JSON response
result = response.json()
# Extract and print the generated text
generated_text = result['choices'][0]['message']['content']
return generated_text
else:
return f"Error: {response.status_code}, {response.text}"
def english_text_to_image(text):
payload = {
"inputs": prompt,
}
response = requests.post(IMAGE_API_URL, headers=img_headers, json=payload)
image_bytes = response.content
image = Image.open(io.BytesIO(image_bytes))
return image
# Custom CSS
st.markdown("""
<style>
/* Background color */
body {
background-color: #f0f0f5;
}
/* Text color and font */
.stApp {
font-family: 'Arial', sans-serif;
color: #333;
}
/* Titles and subtitles styling */
h1 {
color: #2E8B57;
text-align: center;
text-shadow: 2px 2px 5px #aaaaaa;
}
h2, h3 {
color: #4682B4;
text-shadow: 1px 1px 3px #aaaaaa;
}
/* Background texture */
.stApp {
background: linear-gradient(to bottom right, #fff7e6, #e6f7ff);
}
/* Button styling */
button[kind="primary"] {
background-color: #4682B4;
color: white;
border-radius: 8px;
padding: 0.5rem 1rem;
}
button[kind="primary"]:hover {
background-color: #5b9bd5;
}
/* Text area and input field styling */
textarea, input {
border-radius: 10px;
padding: 1rem;
border: 2px solid #ccc;
background-color: #f9f9f9;
}
/* Styling the output boxes */
.stMarkdown {
background-color: #e6f9ff;
padding: 1rem;
border-radius: 10px;
box-shadow: 2px 2px 10px #ccc;
}
</style>
""", unsafe_allow_html=True)
#sidebar styling
st.markdown("""
<style>
[data-testid=stSidebar] {
background-color: #FFFFFF;
margin-right: 20px;
border-right: 2px solid #FFFFFF
}
</style>
""", unsafe_allow_html=True)
#options styling in sidebar and added image in sidebar
with st.sidebar:
selected = option_menu(
menu_title="",
options=['Home','Tool'],
icons=['house-door-fill','setting'],
menu_icon='truck-front-fill',
default_index=0,
styles={
"container": {'padding':'5!important','background-color':'#FAF9F6'},
"icon": {'color':"#000000", "font-size":"23px"},
"nav-link": {'font-size':'16px','text-align':'left','margin':'0px','--hover-color':'#EDEADE','font-weight':'bold'},
"nav-link-selector":{'background-color':'#E6E6FA','font-weight':'bold'}
}
)
if selected == "Home":
# Page title and header
st.title(":blue[Multi-Purpose Tool] - Empowering Educators π")
# Subheader for the app description
st.subheader("A versatile tool designed to assist teachers in translating, summarizing, and visualizing concepts.")
# Main description with detailed information about the app
st.markdown("""
The **Multi-Purpose Tool** is a user-friendly platform developed for educators,
enabling them to enhance their teaching experience. Whether it's translating content
into different languages, summarizing lengthy materials, or visualizing concepts
through images, this tool provides a one-stop solution for modern teaching needs.
### Key Features:
- **Translation**: Translate text seamlessly between languages (e.g., Tamil to English).
- **Summarization**: Quickly generate summaries of long passages for easy understanding.
- **Text to Image**: Visualize difficult concepts by generating images from text descriptions.
### Available Worldwide:
The Multi-Purpose Tool is deployed on Hugging Face and accessible globally to teachers
and educators at the click of a button. Visit the [app here](https://huggingface.co/spaces/Jesivn/Multi_Purpose_Tool).
Empower your classroom with advanced AI tools today!
""")
elif selected=="Tool":
# Row 1: Tamil to English translation
st.subheader("π Translate Tamil to English")
tamil_input = st.text_area("Enter Tamil text", "")
if st.button("Translate"):
english_output = translate_tamil_to_english(tamil_input)
st.markdown(f"**Translated English Text**: {english_output}")
# Row 2: English paragraph summarization
st.subheader("π Summarize English Paragraph")
english_paragraph = st.text_area("Enter English paragraph", "")
if st.button("Summarize"):
summary_output = summarize_english_text(english_paragraph)
st.markdown(f"**Summary**: {summary_output}")
# Row 3: English text to image generation
st.subheader("π¨ Generate Image from English Text")
image_text = st.text_input("Enter description for image generation", "")
if st.button("Generate Image"):
generated_image = english_text_to_image(image_text)
st.image(generated_image, caption="Generated Image")
|