Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -8,6 +8,117 @@ GROQ_API_KEY = os.getenv('GROQ_API_KEY')
|
|
8 |
|
9 |
# API Headers
|
10 |
headers_hf = {"Authorization": f"Bearer {HUGGINGFACE_TOKEN}"}
|
11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
if __name__ == "__main__":
|
13 |
main()
|
|
|
|
8 |
|
9 |
# API Headers
|
10 |
headers_hf = {"Authorization": f"Bearer {HUGGINGFACE_TOKEN}"}
|
11 |
+
headers_groq = {
|
12 |
+
"Authorization": f"Bearer {GROQ_API_KEY}",
|
13 |
+
"Content-Type": "application/json"
|
14 |
+
}
|
15 |
+
|
16 |
+
# Translation Model API URL (Tamil to English)
|
17 |
+
translation_url = "https://api-inference.huggingface.co/models/facebook/mbart-large-50-many-to-one-mmt"
|
18 |
+
|
19 |
+
# Text-to-Image Model API URL
|
20 |
+
image_generation_url = "https://api-inference.huggingface.co/models/black-forest-labs/FLUX.1-schnell"
|
21 |
+
|
22 |
+
# Function to query Hugging Face translation model
|
23 |
+
def translate_text(text):
|
24 |
+
payload = {"inputs": text}
|
25 |
+
response = requests.post(translation_url, headers=headers_hf, json=payload)
|
26 |
+
if response.status_code == 200:
|
27 |
+
result = response.json()
|
28 |
+
translated_text = result[0]['generated_text']
|
29 |
+
return translated_text
|
30 |
+
else:
|
31 |
+
st.error(f"Translation Error {response.status_code}: {response.text}")
|
32 |
+
return None
|
33 |
+
|
34 |
+
# Function to query Groq content generation model
|
35 |
+
def generate_content(english_text, max_tokens, temperature):
|
36 |
+
url = "https://api.groq.com/openai/v1/chat/completions"
|
37 |
+
payload = {
|
38 |
+
"model": "llama-3.1-70b-versatile",
|
39 |
+
"messages": [
|
40 |
+
{"role": "system", "content": "You are a creative and insightful writer."},
|
41 |
+
{"role": "user", "content": f"Write educational content about {english_text} within {max_tokens} tokens."}
|
42 |
+
],
|
43 |
+
"max_tokens": max_tokens,
|
44 |
+
"temperature": temperature
|
45 |
+
}
|
46 |
+
response = requests.post(url, json=payload, headers=headers_groq)
|
47 |
+
if response.status_code == 200:
|
48 |
+
result = response.json()
|
49 |
+
return result['choices'][0]['message']['content']
|
50 |
+
else:
|
51 |
+
st.error(f"Content Generation Error: {response.status_code}")
|
52 |
+
return None
|
53 |
+
|
54 |
+
# Function to generate image prompt
|
55 |
+
def generate_image_prompt(english_text):
|
56 |
+
payload = {
|
57 |
+
"model": "mixtral-8x7b-32768",
|
58 |
+
"messages": [
|
59 |
+
{"role": "system", "content": "You are a professional Text to image prompt generator."},
|
60 |
+
{"role": "user", "content": f"Create a text to image generation prompt about {english_text} within 30 tokens."}
|
61 |
+
],
|
62 |
+
"max_tokens": 30
|
63 |
+
}
|
64 |
+
response = requests.post("https://api.groq.com/openai/v1/chat/completions", json=payload, headers=headers_groq)
|
65 |
+
if response.status_code == 200:
|
66 |
+
result = response.json()
|
67 |
+
return result['choices'][0]['message']['content']
|
68 |
+
else:
|
69 |
+
st.error(f"Prompt Generation Error: {response.status_code}")
|
70 |
+
return None
|
71 |
+
|
72 |
+
# Function to generate an image from the prompt
|
73 |
+
def generate_image(image_prompt):
|
74 |
+
data = {"inputs": image_prompt}
|
75 |
+
response = requests.post(image_generation_url, headers=headers_hf, json=data)
|
76 |
+
if response.status_code == 200:
|
77 |
+
return response.content
|
78 |
+
else:
|
79 |
+
st.error(f"Image Generation Error {response.status_code}: {response.text}")
|
80 |
+
return None
|
81 |
+
|
82 |
+
# Main Streamlit app
|
83 |
+
def main():
|
84 |
+
st.title("Multimodal Generator")
|
85 |
+
|
86 |
+
# Sidebar for temperature and token adjustment
|
87 |
+
st.sidebar.header("Settings")
|
88 |
+
temperature = st.sidebar.slider("Select Temperature", 0.1, 1.0, 0.7)
|
89 |
+
max_tokens = st.sidebar.slider("Max Tokens for Content Generation", 100, 300, 200)
|
90 |
+
|
91 |
+
# Suggested inputs
|
92 |
+
st.write("## Suggested Inputs")
|
93 |
+
suggestions = ["தரவு அறிவியல்", "புதிய திறன்களைக் கற்றுக்கொள்வது எப்படி", "ராக்கெட் எப்படி வேலை செய்கிறது"]
|
94 |
+
selected_suggestion = st.selectbox("Select a suggestion or enter your own:", [""] + suggestions)
|
95 |
+
|
96 |
+
# Input box for user
|
97 |
+
tamil_input = st.text_input("Enter Tamil text (or select a suggestion):", selected_suggestion)
|
98 |
+
|
99 |
+
if st.button("Generate"):
|
100 |
+
# Step 1: Translation (Tamil to English)
|
101 |
+
if tamil_input:
|
102 |
+
st.write("### Translated English Text:")
|
103 |
+
english_text = translate_text(tamil_input)
|
104 |
+
if english_text:
|
105 |
+
st.success(english_text)
|
106 |
+
|
107 |
+
# Step 2: Generate Educational Content
|
108 |
+
st.write("### Generated Educational Content:")
|
109 |
+
with st.spinner('Generating content...'):
|
110 |
+
content_output = generate_content(english_text, max_tokens, temperature)
|
111 |
+
if content_output:
|
112 |
+
st.success(content_output)
|
113 |
+
|
114 |
+
# Step 3: Generate Image from the prompt
|
115 |
+
st.write("### Generated Image:")
|
116 |
+
with st.spinner('Generating image...'):
|
117 |
+
image_prompt = generate_image_prompt(english_text)
|
118 |
+
image_data = generate_image(image_prompt)
|
119 |
+
if image_data:
|
120 |
+
st.image(image_data, caption="Generated Image")
|
121 |
+
|
122 |
if __name__ == "__main__":
|
123 |
main()
|
124 |
+
|