Update app.py
Browse files
app.py
CHANGED
|
@@ -1,10 +1,12 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import os
|
| 3 |
-
os.environ["KERAS_BACKEND"] = "tensorflow"
|
| 4 |
import keras
|
| 5 |
import keras_nlp
|
| 6 |
import utils
|
| 7 |
|
|
|
|
|
|
|
| 8 |
css = """
|
| 9 |
html, body {
|
| 10 |
margin: 0;
|
|
@@ -12,7 +14,6 @@ html, body {
|
|
| 12 |
height: 100%;
|
| 13 |
overflow: hidden;
|
| 14 |
}
|
| 15 |
-
|
| 16 |
body::before {
|
| 17 |
content: '';
|
| 18 |
position: fixed;
|
|
@@ -23,54 +24,53 @@ body::before {
|
|
| 23 |
background-image: url('https://stsci-opo.org/STScI-01J5E849R5W27ZZ2C3QAE9ET75.png');
|
| 24 |
background-size: cover;
|
| 25 |
background-repeat: no-repeat;
|
| 26 |
-
opacity: 0.35;
|
| 27 |
background-position: center;
|
| 28 |
-
z-index: -1;
|
| 29 |
}
|
| 30 |
.gradio-container {
|
| 31 |
display: flex;
|
| 32 |
justify-content: center;
|
| 33 |
align-items: center;
|
| 34 |
-
height: 100vh;
|
| 35 |
}
|
| 36 |
"""
|
| 37 |
|
| 38 |
-
#
|
| 39 |
gemma_lm = keras_nlp.models.CausalLM.from_preset("hf://sultan-hassan/CosmoGemma_2b_en")
|
| 40 |
-
|
| 41 |
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
'What could be the cause of remaining excess power in a signal after foreground mitigation?',
|
| 45 |
-
'What type of coupling in inflationary models can affect the prediction of inflationary parameters?',
|
| 46 |
-
'Please list three ideas for interesting projects to work on to advance cosmology?',
|
| 47 |
-
'Please answer the following multiple-choice question.\n\nQuestion: What is the primary goal of calibrating subgrid feedback models in cosmological simulations?\nA) To ensure that simulations accurately reproduce the observed properties of the interstellar medium.\nB) To create a diverse range of galaxy morphologies in the simulations.\nC) To achieve convergence in simulation results across different resolutions and box sizes.\nD) To steer simulations towards producing a broadly realistic galaxy population that is consistent with key observational constraints.\n\n Give only one answer, either A, B, C, or D, but not more than one, and always give an answer.',
|
| 48 |
-
]
|
| 49 |
|
| 50 |
def launch(message):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
|
| 52 |
-
#
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
# This is to convert QA system to a chatbot by adding history to prompt
|
| 64 |
-
#chat_history = chat.send_message(message) + "\n\n##### History #####\n\n" + chat.get_history()
|
| 65 |
-
#return chat_history
|
| 66 |
|
| 67 |
iface = gr.Interface(launch,
|
| 68 |
inputs="text",
|
| 69 |
outputs="text",
|
| 70 |
examples=examples,
|
| 71 |
css=css,
|
| 72 |
-
title="
|
| 73 |
-
description="
|
| 74 |
|
| 75 |
iface.launch()
|
| 76 |
-
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import os
|
| 3 |
+
os.environ["KERAS_BACKEND"] = "tensorflow"
|
| 4 |
import keras
|
| 5 |
import keras_nlp
|
| 6 |
import utils
|
| 7 |
|
| 8 |
+
from googletrans import Translator
|
| 9 |
+
|
| 10 |
css = """
|
| 11 |
html, body {
|
| 12 |
margin: 0;
|
|
|
|
| 14 |
height: 100%;
|
| 15 |
overflow: hidden;
|
| 16 |
}
|
|
|
|
| 17 |
body::before {
|
| 18 |
content: '';
|
| 19 |
position: fixed;
|
|
|
|
| 24 |
background-image: url('https://stsci-opo.org/STScI-01J5E849R5W27ZZ2C3QAE9ET75.png');
|
| 25 |
background-size: cover;
|
| 26 |
background-repeat: no-repeat;
|
| 27 |
+
opacity: 0.35;
|
| 28 |
background-position: center;
|
| 29 |
+
z-index: -1;
|
| 30 |
}
|
| 31 |
.gradio-container {
|
| 32 |
display: flex;
|
| 33 |
justify-content: center;
|
| 34 |
align-items: center;
|
| 35 |
+
height: 100vh;
|
| 36 |
}
|
| 37 |
"""
|
| 38 |
|
| 39 |
+
# Модельді жүктеу
|
| 40 |
gemma_lm = keras_nlp.models.CausalLM.from_preset("hf://sultan-hassan/CosmoGemma_2b_en")
|
| 41 |
+
chat = utils.ChatState(gemma_lm)
|
| 42 |
|
| 43 |
+
# Аударма объектісі
|
| 44 |
+
translator = Translator()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
def launch(message):
|
| 47 |
+
# 1. Қолданушы сұрағын ағылшыншаға аударамыз (егер сұрақ қазақша болса ғана)
|
| 48 |
+
detected = translator.detect(message)
|
| 49 |
+
if detected.lang == 'kk' or detected.lang == 'ky': # қазақша немесе қырғызша болса
|
| 50 |
+
message_en = translator.translate(message, src='kk', dest='en').text
|
| 51 |
+
else:
|
| 52 |
+
message_en = message
|
| 53 |
+
|
| 54 |
+
# 2. Модельге ағылшынша сұрақты жібереміз, чат тарихын қосып
|
| 55 |
+
response_en = chat.send_message(message_en)
|
| 56 |
|
| 57 |
+
# 3. Модельдің ағылшынша жауабын қазақшаға аударамыз
|
| 58 |
+
response_kk = translator.translate(response_en, src='en', dest='kk').text
|
| 59 |
+
|
| 60 |
+
return response_kk
|
| 61 |
+
|
| 62 |
+
examples = [
|
| 63 |
+
'Космологиядағы негізгі зерттеу әдістері қандай?',
|
| 64 |
+
'Инфляциялық модельдерде қандай байланыстар инфляция параметрлерін болжауға әсер етеді?',
|
| 65 |
+
'Астрономиядағы сигналдағы артық қуаттың себебі неде болуы мүмкін?',
|
| 66 |
+
]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
|
| 68 |
iface = gr.Interface(launch,
|
| 69 |
inputs="text",
|
| 70 |
outputs="text",
|
| 71 |
examples=examples,
|
| 72 |
css=css,
|
| 73 |
+
title="CosmoGemma қазақша сөйлейді 👋",
|
| 74 |
+
description="CosmoGemma моделін қазақша сұрақтармен пайдаланыңыз, жауаптарды қазақша алыңыз.")
|
| 75 |
|
| 76 |
iface.launch()
|
|
|