aiqcamp commited on
Commit
384e099
ยท
verified ยท
1 Parent(s): 19e8804

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +125 -63
app.py CHANGED
@@ -188,9 +188,8 @@ def search_protein_data(analysis, dataset):
188
  print(f"Error processing entry: {str(e)}")
189
  continue
190
 
191
- # ๊ฒฐ๊ณผ ์ •๋ ฌ ๋ฐ ๋ฐ˜ํ™˜
192
- scored_entries.sort(reverse=True)
193
- return scored_entries[:3]
194
 
195
  except Exception as e:
196
  print(f"๋ฐ์ดํ„ฐ ๊ฒ€์ƒ‰ ์ค‘ ์˜ค๋ฅ˜: {str(e)}")
@@ -944,8 +943,25 @@ def process_chat_and_generate(message, history):
944
 
945
  # 2. ํ”„๋กฌํ”„ํŠธ ๋ถ„์„
946
  analysis = analyze_prompt(message)
 
 
 
 
 
 
947
  similar_structures = search_protein_data(analysis, ds)
 
 
 
 
 
 
948
  params = extract_parameters(analysis, similar_structures)
 
 
 
 
 
949
 
950
  # 3. ๋ถ„์„ ๊ฒฐ๊ณผ ์ถ”๊ฐ€ (์ด์ „ ๋ฉ”์‹œ์ง€ ์œ ์ง€)
951
  current_history = current_history[:-1] + [
@@ -959,71 +975,115 @@ def process_chat_and_generate(message, history):
959
  yield (current_history, None, None, None, None, None, None)
960
 
961
  # 4. ๋‹จ๋ฐฑ์งˆ ์ƒ์„ฑ
962
- generator = protein_diffusion_model(...) # ๊ธฐ์กด ํŒŒ๋ผ๋ฏธํ„ฐ ์œ ์ง€
963
-
964
- # 5. ์ƒ์„ฑ ๊ณผ์ • ์ถ”์  (์ด์ „ ๋ฉ”์‹œ์ง€๋“ค ์œ ์ง€)
965
- step = 0
966
- for result in generator:
967
- step += 1
968
- progress_msg = f"๋‹จ๋ฐฑ์งˆ ์ƒ์„ฑ ์ค‘... {step}/25 ๋‹จ๊ณ„ ์™„๋ฃŒ"
969
- current_history = current_history[:-1] + [
970
- {"role": "assistant", "content": progress_msg}
971
- ]
972
- yield (
973
- current_history,
974
- create_radar_chart(calculate_hero_stats(
975
- params['helix_bias'],
976
- params['strand_bias'],
977
- params['loop_bias'],
978
- float(params['hydrophobic_target_score'])
979
- )),
980
- progress_msg,
981
- result[0],
982
- result[1],
983
- result[2],
984
- result[3]
985
  )
986
 
987
- # 6. ์ตœ์ข… ๊ฒฐ๊ณผ ๋ฐ ์„ค๋ช… ์ถ”๊ฐ€ (๋ชจ๋“  ์ด์ „ ๋ฉ”์‹œ์ง€ ์œ ์ง€)
988
- final_explanation = f"""
989
- ๋‹จ๋ฐฑ์งˆ ์„ค๊ณ„๊ฐ€ ์™„๋ฃŒ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
990
 
991
- [๋ถ„์„ ๊ฒฐ๊ณผ]
992
- {analysis}
993
 
994
- [๊ตฌ์กฐ์  ํŠน์ง•]
995
- - ๊ธธ์ด: {params['sequence_length']} ์•„๋ฏธ๋…ธ์‚ฐ
996
- - ์•ŒํŒŒ ํ—ฌ๋ฆญ์Šค ๋น„์œจ: {params['helix_bias']*100:.1f}%
997
- - ๋ฒ ํƒ€ ์‹œํŠธ ๋น„์œจ: {params['strand_bias']*100:.1f}%
998
- - ๋ฃจํ”„ ๊ตฌ์กฐ ๋น„์œจ: {params['loop_bias']*100:.1f}%
999
- - ์†Œ์ˆ˜์„ฑ ์ ์ˆ˜: {params['hydrophobic_target_score']}
1000
 
1001
- [์ƒ์„ฑ ๊ณผ์ •]
1002
- - ์ด {step}๋‹จ๊ณ„์˜ ์ตœ์ ํ™” ์™„๋ฃŒ
1003
- - ์ตœ์ข… ์•ˆ์ •์„ฑ ์ ์ˆ˜: {np.mean(plddt_data) if plddt_data else 0:.2f}
1004
- - ์ฐธ์กฐ๋œ ์œ ์‚ฌ ๊ตฌ์กฐ: {len(similar_structures)}๊ฐœ
1005
 
1006
- 3D ๊ตฌ์กฐ์™€ ์ƒ์„ธ ๋ถ„์„ ๊ฒฐ๊ณผ๋ฅผ ํ™•์ธํ•˜์‹ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
1007
- """
 
 
 
 
1008
 
1009
- final_history = current_history + [
1010
- {"role": "assistant", "content": final_explanation}
1011
- ]
 
1012
 
1013
- return (
1014
- final_history, # ๋ชจ๋“  ๋Œ€ํ™” ๊ธฐ๋ก ์œ ์ง€
1015
- create_radar_chart(stats),
1016
- final_explanation,
1017
- output_seq,
1018
- output_pdb,
1019
- structure_view,
1020
- plddt_plot
1021
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1022
 
1023
  except Exception as e:
 
1024
  print(f"Error in process_chat_and_generate: {str(e)}")
1025
  traceback.print_exc()
1026
- error_msg = f"์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค: {str(e)}"
1027
  return (
1028
  history + [
1029
  {"role": "user", "content": message},
@@ -1032,10 +1092,6 @@ def process_chat_and_generate(message, history):
1032
  None, None, None, None, None, None
1033
  )
1034
 
1035
- # ํ•„์š”ํ•œ ๋ชจ๋“ˆ ์ถ”๊ฐ€
1036
- import json
1037
- from Bio import SeqIO, Align
1038
- from Bio.Seq import Seq
1039
 
1040
  # ์‹œ์ž‘ ๋ถ€๋ถ„์— ์ถ”๊ฐ€
1041
  def extract_keywords(analysis):
@@ -1066,9 +1122,10 @@ def calculate_similarity(keywords, entry):
1066
  """ํ‚ค์›Œ๋“œ์™€ ๋ฐ์ดํ„ฐ์…‹ ํ•ญ๋ชฉ ๊ฐ„์˜ ์œ ์‚ฌ๋„ ๊ณ„์‚ฐ"""
1067
  try:
1068
  score = 0
1069
- if not entry or not isinstance(entry, dict):
1070
  return 0
1071
 
 
1072
  sequence = entry.get('sequence', '').lower()
1073
  description = entry.get('description', '')
1074
 
@@ -1090,8 +1147,13 @@ def calculate_similarity(keywords, entry):
1090
  print(f"์œ ์‚ฌ๋„ ๊ณ„์‚ฐ ์ค‘ ์˜ค๋ฅ˜: {str(e)}")
1091
  return 0
1092
 
1093
- # plddt_data ๋ณ€์ˆ˜ ์ถ”๊ฐ€
1094
  plddt_data = []
 
 
 
 
 
1095
 
1096
  # ์˜ˆ์ œ ํ”„๋กฌํ”„ํŠธ ์ฒ˜๋ฆฌ ํ•จ์ˆ˜ ์ˆ˜์ •
1097
  def use_example(example):
@@ -1665,4 +1727,4 @@ with gr.Blocks(theme='ParityError/Interstellar') as demo:
1665
 
1666
  # ์‹คํ–‰
1667
  demo.queue()
1668
- demo.launch(debug=True)
 
188
  print(f"Error processing entry: {str(e)}")
189
  continue
190
 
191
+ # ๊ฒฐ๊ณผ ์ •๋ ฌ ๋ฐ ๋ฐ˜ํ™˜ (key ํ•จ์ˆ˜ ์‚ฌ์šฉ)
192
+ return sorted(scored_entries, key=lambda x: x[0], reverse=True)[:3]
 
193
 
194
  except Exception as e:
195
  print(f"๋ฐ์ดํ„ฐ ๊ฒ€์ƒ‰ ์ค‘ ์˜ค๋ฅ˜: {str(e)}")
 
943
 
944
  # 2. ํ”„๋กฌํ”„ํŠธ ๋ถ„์„
945
  analysis = analyze_prompt(message)
946
+ if not analysis:
947
+ return history + [
948
+ {"role": "user", "content": message},
949
+ {"role": "assistant", "content": "์š”์ฒญ ๋ถ„์„์— ์‹คํŒจํ–ˆ์Šต๋‹ˆ๋‹ค."}
950
+ ], None, None, None, None, None, None
951
+
952
  similar_structures = search_protein_data(analysis, ds)
953
+ if not similar_structures:
954
+ return history + [
955
+ {"role": "user", "content": message},
956
+ {"role": "assistant", "content": "์ ํ•ฉํ•œ ์ฐธ์กฐ ๊ตฌ์กฐ๋ฅผ ์ฐพ์ง€ ๋ชปํ–ˆ์Šต๋‹ˆ๋‹ค."}
957
+ ], None, None, None, None, None, None
958
+
959
  params = extract_parameters(analysis, similar_structures)
960
+ if not params:
961
+ return history + [
962
+ {"role": "user", "content": message},
963
+ {"role": "assistant", "content": "ํŒŒ๋ผ๋ฏธํ„ฐ ์„ค์ •์— ์‹คํŒจํ–ˆ์Šต๋‹ˆ๋‹ค."}
964
+ ], None, None, None, None, None, None
965
 
966
  # 3. ๋ถ„์„ ๊ฒฐ๊ณผ ์ถ”๊ฐ€ (์ด์ „ ๋ฉ”์‹œ์ง€ ์œ ์ง€)
967
  current_history = current_history[:-1] + [
 
975
  yield (current_history, None, None, None, None, None, None)
976
 
977
  # 4. ๋‹จ๋ฐฑ์งˆ ์ƒ์„ฑ
978
+ try:
979
+ generator = protein_diffusion_model(
980
+ sequence=None,
981
+ seq_len=params['sequence_length'],
982
+ helix_bias=params['helix_bias'],
983
+ strand_bias=params['strand_bias'],
984
+ loop_bias=params['loop_bias'],
985
+ secondary_structure=None,
986
+ aa_bias=None,
987
+ aa_bias_potential=None,
988
+ num_steps="25",
989
+ noise="normal",
990
+ hydrophobic_target_score=str(params['hydrophobic_target_score']),
991
+ hydrophobic_potential="2",
992
+ contigs=None,
993
+ pssm=None,
994
+ seq_mask=None,
995
+ str_mask=None,
996
+ rewrite_pdb=None
 
 
 
 
997
  )
998
 
999
+ # 5. ์ƒ์„ฑ ๊ณผ์ • ์ถ”์  (์ด์ „ ๋ฉ”์‹œ์ง€๋“ค ์œ ์ง€)
1000
+ step = 0
1001
+ final_result = None
1002
+ for result in generator:
1003
+ step += 1
1004
+ final_result = result
1005
+ progress_msg = f"๋‹จ๋ฐฑ์งˆ ์ƒ์„ฑ ์ค‘... {step}/25 ๋‹จ๊ณ„ ์™„๋ฃŒ"
1006
+ current_history = current_history[:-1] + [
1007
+ {"role": "assistant", "content": progress_msg}
1008
+ ]
1009
+ yield (
1010
+ current_history,
1011
+ create_radar_chart(calculate_hero_stats(
1012
+ params['helix_bias'],
1013
+ params['strand_bias'],
1014
+ params['loop_bias'],
1015
+ float(params['hydrophobic_target_score'])
1016
+ )),
1017
+ progress_msg,
1018
+ result[0], # output_seq
1019
+ result[1], # output_pdb
1020
+ result[2], # structure_view
1021
+ result[3] # plddt_plot
1022
+ )
1023
 
1024
+ if final_result is None:
1025
+ raise Exception("์ƒ์„ฑ ๊ฒฐ๊ณผ๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค")
1026
 
1027
+ # 6. ์ตœ์ข… ๊ฒฐ๊ณผ ๋ฐ ์„ค๋ช… ์ถ”๊ฐ€
1028
+ output_seq, output_pdb, structure_view, plddt_plot = final_result
1029
+
1030
+ final_explanation = f"""
1031
+ ๋‹จ๋ฐฑ์งˆ ์„ค๊ณ„๊ฐ€ ์™„๋ฃŒ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.
 
1032
 
1033
+ [๋ถ„์„ ๊ฒฐ๊ณผ]
1034
+ {analysis}
 
 
1035
 
1036
+ [๊ตฌ์กฐ์  ํŠน์ง•]
1037
+ - ๊ธธ์ด: {params['sequence_length']} ์•„๋ฏธ๋…ธ์‚ฐ
1038
+ - ์•ŒํŒŒ ํ—ฌ๋ฆญ์Šค ๋น„์œจ: {params['helix_bias']*100:.1f}%
1039
+ - ๋ฒ ํƒ€ ์‹œํŠธ ๋น„์œจ: {params['strand_bias']*100:.1f}%
1040
+ - ๋ฃจํ”„ ๊ตฌ์กฐ ๋น„์œจ: {params['loop_bias']*100:.1f}%
1041
+ - ์†Œ์ˆ˜์„ฑ ์ ์ˆ˜: {params['hydrophobic_target_score']}
1042
 
1043
+ [์ƒ์„ฑ ๊ณผ์ •]
1044
+ - ์ด {step}๋‹จ๊ณ„์˜ ์ตœ์ ํ™” ์™„๋ฃŒ
1045
+ - ์ตœ์ข… ์•ˆ์ •์„ฑ ์ ์ˆ˜: {np.mean(plddt_data) if plddt_data else 0:.2f}
1046
+ - ์ฐธ์กฐ๋œ ์œ ์‚ฌ ๊ตฌ์กฐ: {len(similar_structures)}๊ฐœ
1047
 
1048
+ 3D ๊ตฌ์กฐ์™€ ์ƒ์„ธ ๋ถ„์„ ๊ฒฐ๊ณผ๋ฅผ ํ™•์ธํ•˜์‹ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
1049
+ """
1050
+
1051
+ final_history = current_history + [
1052
+ {"role": "assistant", "content": final_explanation}
1053
+ ]
1054
+
1055
+ stats = calculate_hero_stats(
1056
+ params['helix_bias'],
1057
+ params['strand_bias'],
1058
+ params['loop_bias'],
1059
+ float(params['hydrophobic_target_score'])
1060
+ )
1061
+
1062
+ return (
1063
+ final_history,
1064
+ create_radar_chart(stats),
1065
+ final_explanation,
1066
+ output_seq,
1067
+ output_pdb,
1068
+ structure_view,
1069
+ plddt_plot
1070
+ )
1071
+
1072
+ except Exception as e:
1073
+ error_msg = f"๋‹จ๋ฐฑ์งˆ ์ƒ์„ฑ ์ค‘ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค: {str(e)}"
1074
+ print(error_msg)
1075
+ traceback.print_exc()
1076
+ return (
1077
+ current_history + [
1078
+ {"role": "assistant", "content": error_msg}
1079
+ ],
1080
+ None, None, None, None, None, None
1081
+ )
1082
 
1083
  except Exception as e:
1084
+ error_msg = f"์ฒ˜๋ฆฌ ์ค‘ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค: {str(e)}"
1085
  print(f"Error in process_chat_and_generate: {str(e)}")
1086
  traceback.print_exc()
 
1087
  return (
1088
  history + [
1089
  {"role": "user", "content": message},
 
1092
  None, None, None, None, None, None
1093
  )
1094
 
 
 
 
 
1095
 
1096
  # ์‹œ์ž‘ ๋ถ€๋ถ„์— ์ถ”๊ฐ€
1097
  def extract_keywords(analysis):
 
1122
  """ํ‚ค์›Œ๋“œ์™€ ๋ฐ์ดํ„ฐ์…‹ ํ•ญ๋ชฉ ๊ฐ„์˜ ์œ ์‚ฌ๋„ ๊ณ„์‚ฐ"""
1123
  try:
1124
  score = 0
1125
+ if not isinstance(entry, dict):
1126
  return 0
1127
 
1128
+ # ์•ˆ์ „ํ•œ ์ ‘๊ทผ์„ ์œ„ํ•œ get ๋ฉ”์„œ๋“œ ์‚ฌ์šฉ
1129
  sequence = entry.get('sequence', '').lower()
1130
  description = entry.get('description', '')
1131
 
 
1147
  print(f"์œ ์‚ฌ๋„ ๊ณ„์‚ฐ ์ค‘ ์˜ค๋ฅ˜: {str(e)}")
1148
  return 0
1149
 
1150
+ # ์ „์—ญ ๋ณ€์ˆ˜ ์ •์˜
1151
  plddt_data = []
1152
+ stats = {} # ๋Šฅ๋ ฅ์น˜ ์ €์žฅ์šฉ
1153
+ output_seq = None
1154
+ output_pdb = None
1155
+ structure_view = None
1156
+ plddt_plot = None
1157
 
1158
  # ์˜ˆ์ œ ํ”„๋กฌํ”„ํŠธ ์ฒ˜๋ฆฌ ํ•จ์ˆ˜ ์ˆ˜์ •
1159
  def use_example(example):
 
1727
 
1728
  # ์‹คํ–‰
1729
  demo.queue()
1730
+ demo.launch(debug=True, share=True)