openfree commited on
Commit
aa3d2c4
ยท
verified ยท
1 Parent(s): 35f212d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -8
app.py CHANGED
@@ -92,13 +92,15 @@ class ProteinAnalyzer:
92
  return ''.join(protein)
93
 
94
  @staticmethod
95
- def analyze_protein_with_llm(protein_sequence: str, cell_type: str) -> str:
96
  """Analyze protein structure and function using Friendli LLM API"""
97
 
98
  # Get API token from environment
99
  token = os.getenv("FRIENDLI_TOKEN")
100
  if not token:
101
  logger.warning("FRIENDLI_TOKEN not found in environment variables")
 
 
102
  return "Protein analysis unavailable: API token not configured"
103
 
104
  try:
@@ -108,8 +110,23 @@ class ProteinAnalyzer:
108
  "Content-Type": "application/json"
109
  }
110
 
111
- # Create prompt for protein analysis
112
- prompt = f"""You are a bioinformatics expert. Analyze the following protein sequence and provide insights about its potential structure and function.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
113
 
114
  Protein sequence: {protein_sequence}
115
  Cell type context: {cell_type}
@@ -128,7 +145,7 @@ Keep the response concise but informative, suitable for display in a scientific
128
  "messages": [
129
  {
130
  "role": "system",
131
- "content": "You are a knowledgeable bioinformatics assistant specializing in protein structure and function prediction."
132
  },
133
  {
134
  "role": "user",
@@ -214,10 +231,10 @@ class DNADiffusionApp:
214
  logger.error(f"Generation failed: {e}")
215
  raise
216
 
217
- def handle_generation_request(self, cell_type: str, guidance_scale: float):
218
  """Handle sequence generation request from Gradio"""
219
  try:
220
- logger.info(f"Generating sequence for cell type: {cell_type}")
221
 
222
  # Generate DNA sequence
223
  sequence, metadata = self.generate_sequence(cell_type, guidance_scale)
@@ -233,7 +250,7 @@ class DNADiffusionApp:
233
  # Analyze protein with LLM
234
  logger.info("Analyzing protein structure and function...")
235
  protein_analysis = self.protein_analyzer.analyze_protein_with_llm(
236
- protein_sequence, cell_type
237
  )
238
 
239
  # Add analysis to metadata
@@ -277,6 +294,7 @@ def create_demo():
277
 
278
  if (event.data.type === 'generate_request') {
279
  console.log('Triggering generation for cell type:', event.data.cellType);
 
280
 
281
  // Update the hidden cell type input
282
  const radioInputs = document.querySelectorAll('#cell-type-input input[type="radio"]');
@@ -288,6 +306,15 @@ def create_demo():
288
  }
289
  });
290
 
 
 
 
 
 
 
 
 
 
291
  // Small delay to ensure radio button update is processed
292
  setTimeout(() => {
293
  document.querySelector('#generate-btn').click();
@@ -340,6 +367,12 @@ def create_demo():
340
  label="Cell Type",
341
  elem_id="cell-type-input"
342
  )
 
 
 
 
 
 
343
  guidance_input = gr.Slider(
344
  minimum=1.0,
345
  maximum=10.0,
@@ -366,7 +399,7 @@ def create_demo():
366
  # Wire up the generation
367
  generate_btn.click(
368
  fn=app.handle_generation_request,
369
- inputs=[cell_type_input, guidance_input],
370
  outputs=[sequence_output, metadata_output]
371
  ).then(
372
  fn=None,
 
92
  return ''.join(protein)
93
 
94
  @staticmethod
95
+ def analyze_protein_with_llm(protein_sequence: str, cell_type: str, language: str = "en") -> str:
96
  """Analyze protein structure and function using Friendli LLM API"""
97
 
98
  # Get API token from environment
99
  token = os.getenv("FRIENDLI_TOKEN")
100
  if not token:
101
  logger.warning("FRIENDLI_TOKEN not found in environment variables")
102
+ if language == "ko":
103
+ return "๋‹จ๋ฐฑ์งˆ ๋ถ„์„ ๋ถˆ๊ฐ€: API ํ† ํฐ์ด ์„ค์ •๋˜์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค"
104
  return "Protein analysis unavailable: API token not configured"
105
 
106
  try:
 
110
  "Content-Type": "application/json"
111
  }
112
 
113
+ # Create prompt for protein analysis based on language
114
+ if language == "ko":
115
+ prompt = f"""๋‹น์‹ ์€ ์ƒ๋ฌผ์ •๋ณดํ•™ ์ „๋ฌธ๊ฐ€์ž…๋‹ˆ๋‹ค. ๋‹ค์Œ ๋‹จ๋ฐฑ์งˆ ์„œ์—ด์„ ๋ถ„์„ํ•˜๊ณ  ์ž ์žฌ์ ์ธ ๊ตฌ์กฐ์™€ ๊ธฐ๋Šฅ์— ๋Œ€ํ•œ ํ†ต์ฐฐ๋ ฅ์„ ์ œ๊ณตํ•ด์ฃผ์„ธ์š”.
116
+
117
+ ๋‹จ๋ฐฑ์งˆ ์„œ์—ด: {protein_sequence}
118
+ ์„ธํฌ ์œ ํ˜•: {cell_type}
119
+
120
+ ๋‹ค์Œ ๋‚ด์šฉ์„ ํฌํ•จํ•ด์ฃผ์„ธ์š”:
121
+ 1. ์„œ์—ด ํŒจํ„ด์„ ๊ธฐ๋ฐ˜์œผ๋กœ ์˜ˆ์ธก๋˜๋Š” ๋‹จ๋ฐฑ์งˆ ํŒจ๋ฐ€๋ฆฌ ๋˜๋Š” ๋„๋ฉ”์ธ
122
+ 2. ์ž ์žฌ์ ์ธ ๊ตฌ์กฐ์  ํŠน์ง• (์•ŒํŒŒ ๋‚˜์„ , ๋ฒ ํƒ€ ์‹œํŠธ, ๋ฃจํ”„)
123
+ 3. ๊ฐ€๋Šฅํ•œ ์ƒ๋ฌผํ•™์  ๊ธฐ๋Šฅ
124
+ 4. {cell_type} ์„ธํฌ ์œ ํ˜•๊ณผ์˜ ๊ด€๋ จ์„ฑ
125
+ 5. ์ฃผ๋ชฉํ•  ๋งŒํ•œ ์„œ์—ด ๋ชจํ‹ฐํ”„๋‚˜ ํŠน์„ฑ
126
+
127
+ ๊ณผํ•™ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์— ํ‘œ์‹œํ•˜๊ธฐ์— ์ ํ•ฉํ•˜๋„๋ก ๊ฐ„๊ฒฐํ•˜๋ฉด์„œ๋„ ์œ ์ตํ•œ ์‘๋‹ต์„ ์ž‘์„ฑํ•ด์ฃผ์„ธ์š”."""
128
+ else:
129
+ prompt = f"""You are a bioinformatics expert. Analyze the following protein sequence and provide insights about its potential structure and function.
130
 
131
  Protein sequence: {protein_sequence}
132
  Cell type context: {cell_type}
 
145
  "messages": [
146
  {
147
  "role": "system",
148
+ "content": "You are a knowledgeable bioinformatics assistant specializing in protein structure and function prediction." if language == "en" else "๋‹น์‹ ์€ ๋‹จ๋ฐฑ์งˆ ๊ตฌ์กฐ์™€ ๊ธฐ๋Šฅ ์˜ˆ์ธก์„ ์ „๋ฌธ์œผ๋กœ ํ•˜๋Š” ์ง€์‹์ด ํ’๋ถ€ํ•œ ์ƒ๋ฌผ์ •๋ณดํ•™ ์–ด์‹œ์Šคํ„ดํŠธ์ž…๋‹ˆ๋‹ค."
149
  },
150
  {
151
  "role": "user",
 
231
  logger.error(f"Generation failed: {e}")
232
  raise
233
 
234
+ def handle_generation_request(self, cell_type: str, guidance_scale: float, language: str = "en"):
235
  """Handle sequence generation request from Gradio"""
236
  try:
237
+ logger.info(f"Generating sequence for cell type: {cell_type}, language: {language}")
238
 
239
  # Generate DNA sequence
240
  sequence, metadata = self.generate_sequence(cell_type, guidance_scale)
 
250
  # Analyze protein with LLM
251
  logger.info("Analyzing protein structure and function...")
252
  protein_analysis = self.protein_analyzer.analyze_protein_with_llm(
253
+ protein_sequence, cell_type, language
254
  )
255
 
256
  # Add analysis to metadata
 
294
 
295
  if (event.data.type === 'generate_request') {
296
  console.log('Triggering generation for cell type:', event.data.cellType);
297
+ console.log('Language:', event.data.language);
298
 
299
  // Update the hidden cell type input
300
  const radioInputs = document.querySelectorAll('#cell-type-input input[type="radio"]');
 
306
  }
307
  });
308
 
309
+ // Update the language input
310
+ const langInputs = document.querySelectorAll('#language-input input[type="radio"]');
311
+ langInputs.forEach(input => {
312
+ if (input.value === event.data.language) {
313
+ input.checked = true;
314
+ input.dispatchEvent(new Event('change'));
315
+ }
316
+ });
317
+
318
  // Small delay to ensure radio button update is processed
319
  setTimeout(() => {
320
  document.querySelector('#generate-btn').click();
 
367
  label="Cell Type",
368
  elem_id="cell-type-input"
369
  )
370
+ language_input = gr.Radio(
371
+ ["en", "ko"],
372
+ value="en",
373
+ label="Language",
374
+ elem_id="language-input"
375
+ )
376
  guidance_input = gr.Slider(
377
  minimum=1.0,
378
  maximum=10.0,
 
399
  # Wire up the generation
400
  generate_btn.click(
401
  fn=app.handle_generation_request,
402
+ inputs=[cell_type_input, guidance_input, language_input],
403
  outputs=[sequence_output, metadata_output]
404
  ).then(
405
  fn=None,