Manyue-DataScientist commited on
Commit
21c5617
·
verified ·
1 Parent(s): a28d3c7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -30
app.py CHANGED
@@ -1,6 +1,6 @@
1
  import streamlit as st
2
  import json
3
- from huggingface_hub import InferenceClient
4
  import time
5
 
6
  # Initialize Streamlit page configuration
@@ -11,18 +11,15 @@ st.set_page_config(
11
  )
12
 
13
  # Secret management
14
- def get_hf_api_token():
15
  # In production, use Streamlit secrets
16
- return st.secrets["HUGGINGFACE_API_TOKEN"]
17
 
18
- # Initialize HF client
19
  @st.cache_resource
20
- def get_hf_client():
21
- client = InferenceClient(
22
- model="meta-llama/Llama-2-7b-chat-hf",
23
- token=get_hf_api_token()
24
- )
25
- return client
26
 
27
  # Load and cache knowledge base
28
  @st.cache_data
@@ -74,9 +71,9 @@ def get_context(query: str, knowledge_base: dict) -> str:
74
 
75
  return "\n".join(contexts)
76
 
77
- def generate_llama_prompt(query: str, context: str) -> str:
78
- """Generate prompt for Llama model"""
79
- return f"""[INST] You are Manyue's AI assistant. Use this context to answer questions about Manyue:
80
 
81
  Context:
82
  {context}
@@ -89,36 +86,35 @@ Instructions:
89
  - Be specific about technical details and achievements
90
  - Keep responses concise but informative
91
  - Focus on relevant experience and skills
92
- - Maintain a professional tone
93
-
94
- Your response: [/INST]"""
95
 
96
  def get_chat_response(query: str, knowledge_base: dict) -> str:
97
- """Get response from Llama model via HF API"""
98
  try:
99
  # Get context
100
  context = get_context(query, knowledge_base)
101
 
102
  # Generate prompt
103
- prompt = generate_llama_prompt(query, context)
104
 
105
- # Get client
106
- client = get_hf_client()
107
 
108
  # Generate response
109
- response = client.text_generation(
110
- prompt,
111
- max_new_tokens=200,
112
- temperature=0.7,
113
- top_p=0.95,
114
- repetition_penalty=1.1
 
 
115
  )
116
 
117
- # Clean response
118
- response = response.strip()
119
- response = response.split("[/INST]")[-1].strip()
120
 
121
- return response
122
 
123
  except Exception as e:
124
  st.error(f"Error generating response: {str(e)}")
 
1
  import streamlit as st
2
  import json
3
+ import openai
4
  import time
5
 
6
  # Initialize Streamlit page configuration
 
11
  )
12
 
13
  # Secret management
14
+ def get_openai_api_token():
15
  # In production, use Streamlit secrets
16
+ return st.secrets["OPENAI_API_KEY"]
17
 
18
+ # Initialize OpenAI client
19
  @st.cache_resource
20
+ def get_openai_client():
21
+ openai.api_key = get_openai_api_token()
22
+ return openai
 
 
 
23
 
24
  # Load and cache knowledge base
25
  @st.cache_data
 
71
 
72
  return "\n".join(contexts)
73
 
74
+ def generate_openai_prompt(query: str, context: str) -> str:
75
+ """Generate prompt for OpenAI model"""
76
+ return f"""You are Manyue's AI assistant. Use this context to answer questions about Manyue:
77
 
78
  Context:
79
  {context}
 
86
  - Be specific about technical details and achievements
87
  - Keep responses concise but informative
88
  - Focus on relevant experience and skills
89
+ - Maintain a professional tone"""
 
 
90
 
91
  def get_chat_response(query: str, knowledge_base: dict) -> str:
92
+ """Get response from OpenAI API"""
93
  try:
94
  # Get context
95
  context = get_context(query, knowledge_base)
96
 
97
  # Generate prompt
98
+ prompt = generate_openai_prompt(query, context)
99
 
100
+ # Get OpenAI client
101
+ client = get_openai_client()
102
 
103
  # Generate response
104
+ response = client.ChatCompletion.create(
105
+ model="gpt-3.5-turbo", # You can change to gpt-4 if preferred
106
+ messages=[
107
+ {"role": "system", "content": prompt},
108
+ {"role": "user", "content": query}
109
+ ],
110
+ max_tokens=200,
111
+ temperature=0.7
112
  )
113
 
114
+ # Extract and clean response
115
+ response_text = response.choices[0].message.content.strip()
 
116
 
117
+ return response_text
118
 
119
  except Exception as e:
120
  st.error(f"Error generating response: {str(e)}")