Nurisslam commited on
Commit
44e2851
·
verified ·
1 Parent(s): 3599981

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -31
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; /* Faint background image */
27
  background-position: center;
28
- z-index: -1; /* Keep the background behind text */
29
  }
30
  .gradio-container {
31
  display: flex;
32
  justify-content: center;
33
  align-items: center;
34
- height: 100vh; /* Ensure the content is vertically centered */
35
  }
36
  """
37
 
38
- #keras.config.set_floatx("bfloat16") uncomment to run at half precision.
39
  gemma_lm = keras_nlp.models.CausalLM.from_preset("hf://sultan-hassan/CosmoGemma_2b_en")
40
- #chat = utils.ChatState(gemma_lm) # adding a chat helper to manage the conversation state (modified version from https://github.com/google-gemini/gemma-cookbook/blob/main/Gemma/Keras_Gemma_2_Quickstart_Chat.ipynb)
41
 
42
- examples = [
43
- 'What are some common methods for model selection in astrophysics?',
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
- # Uncomment for QA system without chat history/memory
53
- #
54
- template = "Instruction:\n{instruction}\n\nResponse:\n{response}"
55
- prompt = template.format(
56
- instruction=message,
57
- response="",
58
- )
59
- out = gemma_lm.generate(prompt, max_length=1024)
60
- ind = out.index('\n\nResponse:\n') + len('\n\nResponse:\n')
61
- return out[ind:]
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="Hey I am CosmoGemma 👋 I can answer cosmology questions from astroph.CO research articles. Try me :)",
73
- description="Gemma_2b_en fine-tuned on QA pairs (~3.5k) generated from Cosmology and Nongalactic Astrophysics articles (arXiv astro-ph.CO) from 2018-2022 and tested on QA pairs (~1k) generated from 2023 articles, scoring over 75% accuracy.")
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()