biftekpatates commited on
Commit
e842470
·
verified ·
1 Parent(s): 3bf2a8e

Upload 5 files

Browse files
Files changed (5) hide show
  1. README.md +6 -5
  2. app.py +109 -0
  3. gitattributes +35 -0
  4. gitignore +176 -0
  5. requirements.txt +3 -0
README.md CHANGED
@@ -1,12 +1,13 @@
1
  ---
2
- title: Module3
3
- emoji: 🦀
4
- colorFrom: red
5
- colorTo: pink
6
  sdk: gradio
7
- sdk_version: 5.17.0
8
  app_file: app.py
9
  pinned: false
 
10
  ---
11
 
12
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
1
  ---
2
+ title: Module 3
3
+ emoji: 👁
4
+ colorFrom: green
5
+ colorTo: blue
6
  sdk: gradio
7
+ sdk_version: 5.14.0
8
  app_file: app.py
9
  pinned: false
10
+ license: apache-2.0
11
  ---
12
 
13
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
app.py ADDED
@@ -0,0 +1,109 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import gradio as gr
3
+ from dotenv import load_dotenv
4
+ from openai import OpenAI
5
+ from prompts.initial_prompt import INITIAL_PROMPT
6
+ from prompts.main_prompt import MAIN_PROMPT
7
+
8
+ # .env 파일에서 OPENAI_API_KEY 로드
9
+ if os.path.exists(".env"):
10
+ load_dotenv(".env")
11
+
12
+ OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
13
+
14
+ client = OpenAI(api_key=OPENAI_API_KEY)
15
+
16
+
17
+
18
+ def gpt_call(history, user_message,
19
+ model="gpt-4o-mini",
20
+ max_tokens=512,
21
+ temperature=0.7,
22
+ top_p=0.95):
23
+ """
24
+ OpenAI ChatCompletion API를 통해 답변을 생성하는 함수.
25
+ - history: [(user_text, assistant_text), ...]
26
+ - user_message: 사용자가 방금 입력한 메시지
27
+ """
28
+ # 1) 시스템 메시지(=MAIN_PROMPT)를 가장 앞에 추가
29
+ messages = [{"role": "system", "content": MAIN_PROMPT}]
30
+
31
+ # 2) 기존 대화 기록(history)을 OpenAI 형식으로 변환
32
+ # user_text -> 'user' / assistant_text -> 'assistant'
33
+ for user_text, assistant_text in history:
34
+ if user_text:
35
+ messages.append({"role": "user", "content": user_text})
36
+ if assistant_text:
37
+ messages.append({"role": "assistant", "content": assistant_text})
38
+
39
+ # 3) 마지막에 이번 사용자의 입력을 추가
40
+ messages.append({"role": "user", "content": user_message})
41
+
42
+ # 4) OpenAI API 호출
43
+ completion = client.chat.completions.create(
44
+ model=model,
45
+ messages=messages,
46
+ max_tokens=max_tokens,
47
+ temperature=temperature,
48
+ top_p=top_p
49
+ )
50
+ return completion.choices[0].message.content
51
+
52
+ def respond(user_message, history):
53
+ """
54
+ Gradio 상에서 submit할 때 호출되는 함수
55
+ - user_message: 사용자가 방금 친 메시지
56
+ - history: 기존 (user, assistant) 튜플 리스트
57
+ """
58
+ # 사용자가 빈 문자열을 보냈다면 아무 일도 하지 않음
59
+ if not user_message:
60
+ return "", history
61
+
62
+ # GPT 모델로부터 응답을 받음
63
+ assistant_reply = gpt_call(history, user_message)
64
+
65
+ # history에 (user, assistant) 쌍 추가
66
+ history.append((user_message, assistant_reply))
67
+
68
+ # Gradio에서는 (새로 비워질 입력창, 갱신된 history)를 반환
69
+ return "", history
70
+
71
+ ##############################
72
+ # Gradio Blocks UI
73
+ ##############################
74
+ with gr.Blocks() as demo:
75
+ gr.Markdown("## Simple Chat Interface")
76
+
77
+ # Chatbot 초기 상태를 설정
78
+ # 첫 번째 메시지는 (user="", assistant=INITIAL_PROMPT) 형태로 넣어
79
+ # 화면상에서 'assistant'가 INITIAL_PROMPT를 말한 것처럼 보이게 함
80
+ chatbot = gr.Chatbot(
81
+ value=[("", INITIAL_PROMPT)], # (user, assistant)
82
+ height=500
83
+ )
84
+
85
+ # (user, assistant) 쌍을 저장할 히스토리 상태
86
+ # 여기서도 동일한 초기 상태를 넣어줌
87
+ state_history = gr.State([("", INITIAL_PROMPT)])
88
+
89
+ # 사용자 입력
90
+ user_input = gr.Textbox(
91
+ placeholder="Type your message here...",
92
+ label="Your Input"
93
+ )
94
+
95
+ # 입력이 submit되면 respond() 호출 → 출력은 (새 입력창, 갱신된 chatbot)
96
+ user_input.submit(
97
+ respond,
98
+ inputs=[user_input, state_history],
99
+ outputs=[user_input, chatbot]
100
+ ).then(
101
+ # respond 끝난 뒤, 최신 history를 state_history에 반영
102
+ fn=lambda _, h: h,
103
+ inputs=[user_input, chatbot],
104
+ outputs=[state_history]
105
+ )
106
+
107
+ # 메인 실행
108
+ if __name__ == "__main__":
109
+ demo.launch(server_name="0.0.0.0", server_port=7860, share=True)
gitattributes ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ *.7z filter=lfs diff=lfs merge=lfs -text
2
+ *.arrow filter=lfs diff=lfs merge=lfs -text
3
+ *.bin filter=lfs diff=lfs merge=lfs -text
4
+ *.bz2 filter=lfs diff=lfs merge=lfs -text
5
+ *.ckpt filter=lfs diff=lfs merge=lfs -text
6
+ *.ftz filter=lfs diff=lfs merge=lfs -text
7
+ *.gz filter=lfs diff=lfs merge=lfs -text
8
+ *.h5 filter=lfs diff=lfs merge=lfs -text
9
+ *.joblib filter=lfs diff=lfs merge=lfs -text
10
+ *.lfs.* filter=lfs diff=lfs merge=lfs -text
11
+ *.mlmodel filter=lfs diff=lfs merge=lfs -text
12
+ *.model filter=lfs diff=lfs merge=lfs -text
13
+ *.msgpack filter=lfs diff=lfs merge=lfs -text
14
+ *.npy filter=lfs diff=lfs merge=lfs -text
15
+ *.npz filter=lfs diff=lfs merge=lfs -text
16
+ *.onnx filter=lfs diff=lfs merge=lfs -text
17
+ *.ot filter=lfs diff=lfs merge=lfs -text
18
+ *.parquet filter=lfs diff=lfs merge=lfs -text
19
+ *.pb filter=lfs diff=lfs merge=lfs -text
20
+ *.pickle filter=lfs diff=lfs merge=lfs -text
21
+ *.pkl filter=lfs diff=lfs merge=lfs -text
22
+ *.pt filter=lfs diff=lfs merge=lfs -text
23
+ *.pth filter=lfs diff=lfs merge=lfs -text
24
+ *.rar filter=lfs diff=lfs merge=lfs -text
25
+ *.safetensors filter=lfs diff=lfs merge=lfs -text
26
+ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
27
+ *.tar.* filter=lfs diff=lfs merge=lfs -text
28
+ *.tar filter=lfs diff=lfs merge=lfs -text
29
+ *.tflite filter=lfs diff=lfs merge=lfs -text
30
+ *.tgz filter=lfs diff=lfs merge=lfs -text
31
+ *.wasm filter=lfs diff=lfs merge=lfs -text
32
+ *.xz filter=lfs diff=lfs merge=lfs -text
33
+ *.zip filter=lfs diff=lfs merge=lfs -text
34
+ *.zst filter=lfs diff=lfs merge=lfs -text
35
+ *tfevents* filter=lfs diff=lfs merge=lfs -text
gitignore ADDED
@@ -0,0 +1,176 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ .env
2
+
3
+ # Byte-compiled / optimized / DLL files
4
+ __pycache__/
5
+ *.py[cod]
6
+ *$py.class
7
+
8
+ # C extensions
9
+ *.so
10
+
11
+ # Distribution / packaging
12
+ .Python
13
+ build/
14
+ develop-eggs/
15
+ dist/
16
+ downloads/
17
+ eggs/
18
+ .eggs/
19
+ lib/
20
+ lib64/
21
+ parts/
22
+ sdist/
23
+ var/
24
+ wheels/
25
+ share/python-wheels/
26
+ *.egg-info/
27
+ .installed.cfg
28
+ *.egg
29
+ MANIFEST
30
+
31
+ # PyInstaller
32
+ # Usually these files are written by a python script from a template
33
+ # before PyInstaller builds the exe, so as to inject date/other infos into it.
34
+ *.manifest
35
+ *.spec
36
+
37
+ # Installer logs
38
+ pip-log.txt
39
+ pip-delete-this-directory.txt
40
+
41
+ # Unit test / coverage reports
42
+ htmlcov/
43
+ .tox/
44
+ .nox/
45
+ .coverage
46
+ .coverage.*
47
+ .cache
48
+ nosetests.xml
49
+ coverage.xml
50
+ *.cover
51
+ *.py,cover
52
+ .hypothesis/
53
+ .pytest_cache/
54
+ cover/
55
+
56
+ # Translations
57
+ *.mo
58
+ *.pot
59
+
60
+ # Django stuff:
61
+ *.log
62
+ local_settings.py
63
+ db.sqlite3
64
+ db.sqlite3-journal
65
+
66
+ # Flask stuff:
67
+ instance/
68
+ .webassets-cache
69
+
70
+ # Scrapy stuff:
71
+ .scrapy
72
+
73
+ # Sphinx documentation
74
+ docs/_build/
75
+
76
+ # PyBuilder
77
+ .pybuilder/
78
+ target/
79
+
80
+ # Jupyter Notebook
81
+ .ipynb_checkpoints
82
+
83
+ # IPython
84
+ profile_default/
85
+ ipython_config.py
86
+
87
+ # pyenv
88
+ # For a library or package, you might want to ignore these files since the code is
89
+ # intended to run in multiple environments; otherwise, check them in:
90
+ # .python-version
91
+
92
+ # pipenv
93
+ # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
94
+ # However, in case of collaboration, if having platform-specific dependencies or dependencies
95
+ # having no cross-platform support, pipenv may install dependencies that don't work, or not
96
+ # install all needed dependencies.
97
+ #Pipfile.lock
98
+
99
+ # UV
100
+ # Similar to Pipfile.lock, it is generally recommended to include uv.lock in version control.
101
+ # This is especially recommended for binary packages to ensure reproducibility, and is more
102
+ # commonly ignored for libraries.
103
+ #uv.lock
104
+
105
+ # poetry
106
+ # Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
107
+ # This is especially recommended for binary packages to ensure reproducibility, and is more
108
+ # commonly ignored for libraries.
109
+ # https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
110
+ #poetry.lock
111
+
112
+ # pdm
113
+ # Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
114
+ #pdm.lock
115
+ # pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
116
+ # in version control.
117
+ # https://pdm.fming.dev/latest/usage/project/#working-with-version-control
118
+ .pdm.toml
119
+ .pdm-python
120
+ .pdm-build/
121
+
122
+ # PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
123
+ __pypackages__/
124
+
125
+ # Celery stuff
126
+ celerybeat-schedule
127
+ celerybeat.pid
128
+
129
+ # SageMath parsed files
130
+ *.sage.py
131
+
132
+ # Environments
133
+ .env
134
+ .venv
135
+ env/
136
+ venv/
137
+ ENV/
138
+ env.bak/
139
+ venv.bak/
140
+
141
+ # Spyder project settings
142
+ .spyderproject
143
+ .spyproject
144
+
145
+ # Rope project settings
146
+ .ropeproject
147
+
148
+ # mkdocs documentation
149
+ /site
150
+
151
+ # mypy
152
+ .mypy_cache/
153
+ .dmypy.json
154
+ dmypy.json
155
+
156
+ # Pyre type checker
157
+ .pyre/
158
+
159
+ # pytype static type analyzer
160
+ .pytype/
161
+
162
+ # Cython debug symbols
163
+ cython_debug/
164
+
165
+ # PyCharm
166
+ # JetBrains specific template is maintained in a separate JetBrains.gitignore that can
167
+ # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
168
+ # and can be added to the global gitignore or merged into this file. For a more nuclear
169
+ # option (not recommended) you can uncomment the following to ignore the entire idea folder.
170
+ #.idea/
171
+
172
+ # Ruff stuff:
173
+ .ruff_cache/
174
+
175
+ # PyPI configuration file
176
+ .pypirc
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ huggingface_hub==0.25.2
2
+ openai
3
+ python-dotenv