Spaces:
Sleeping
Sleeping
Download app.py from davidlopez/Intro-Python-Tutor-V2: direct link, hf CLI and curl.
- Browser
- Download file 1.09 kB
-
https://huggingface.co/spaces/davidlopez/Intro-Python-Tutor-V2/resolve/b484fc5aa0fc3f15d868d9de7d88cf281c7ca3df/app.py
- Command line
-
hf download hf://spaces/davidlopez/Intro-Python-Tutor-V2@b484fc5aa0fc3f15d868d9de7d88cf281c7ca3df/app.py
-
curl -L -o app.py https://huggingface.co/spaces/davidlopez/Intro-Python-Tutor-V2/resolve/b484fc5aa0fc3f15d868d9de7d88cf281c7ca3df/app.py
1.09 kB
| import json | |
| import os | |
| import gradio as gr | |
| from langchain_openai import ChatOpenAI | |
| from langchain.schema import AIMessage, HumanMessage | |
| os.environ['OPENAI_API_KEY']=os.getenv('OpenAI_KEY') | |
| os.environ["LANGCHAIN_API_KEY"]=os.getenv('LANGCHAIN_API_KEY') | |
| os.environ["LANGCHAIN_TRACING_V2"]="true" | |
| os.environ["LANGCHAIN_ENDPOINT"]="https://api.smith.langchain.com" | |
| os.environ["LANGCHAIN_PROJECT"]=os.getenv('LANGCHAIN_PROJECT') | |
| model = ChatOpenAI(model="gpt-4o-mini") | |
| def predict(message, history): | |
| history_langchain_format = [] | |
| for msg in history: | |
| if msg['role'] == "user": | |
| history_langchain_format.append(HumanMessage(content=msg['content'])) | |
| elif msg['role'] == "assistant": | |
| history_langchain_format.append(AIMessage(content=msg['content'])) | |
| history_langchain_format.append(HumanMessage(content=message)) | |
| gpt_response = model.invoke(history_langchain_format) | |
| return gpt_response.content | |
| demo = gr.ChatInterface( | |
| predict, | |
| type="messages", | |
| fill_width=True, | |
| fill_height=True | |
| ) | |
| demo.launch(share=False) | |