Spaces:
Sleeping
Sleeping
Refactor imports and update AI provider handling in app.py; modify requirements.txt for package consistency
0c71d91 | import os | |
| from groq import Groq | |
| from openai import OpenAI | |
| from anthropic import Anthropic | |
| from google import genai | |
| from google.genai import types | |
| import streamlit as st | |
| from dotenv import load_dotenv | |
| load_dotenv() | |
| st.set_page_config(page_title="strftime AI", page_icon="๐") | |
| with open("static/style.css", encoding="utf-8") as f: | |
| st.markdown(f"<style>{f.read()}</style>", unsafe_allow_html=True) | |
| st.header("strftime AI") | |
| with st.sidebar: | |
| provider = st.radio( | |
| "Choose AI Provider", | |
| options=[ | |
| "Groq", | |
| "Gemini", | |
| "OpenAI", | |
| "Anthropic", | |
| ] | |
| ) | |
| text = st.text_input("Enter datetime text eg. 2023-09-28T15:27:58Z", value="2023-09-28T15:27:58Z") | |
| if text: | |
| prompt = f"""Analyze this datetime string and convert it to strftime format: {text} | |
| Please provide your response in exactly this format: | |
| **strftime format:** `[the strftime format string]` | |
| **Breakdown:** | |
| - `%Y`: 4-digit year | |
| - `%m`: 2-digit month (01-12) | |
| - `%d`: 2-digit day (01-31) | |
| [list each format code used with its meaning] | |
| Be precise and only include the format codes that are actually present in the input datetime string. Do not add extra explanations or variations.""" | |
| if provider == "Groq": | |
| client = Groq(api_key=os.environ.get("GROQ_API_KEY")) | |
| chat_completion = client.chat.completions.create( | |
| messages=[{"role": "user", "content": prompt}], | |
| model="gemma2-9b-it", | |
| max_tokens=1024, | |
| ) | |
| st.markdown(chat_completion.choices[0].message.content) | |
| elif provider == "OpenAI": | |
| client = OpenAI(api_key=os.getenv("OPENAI_API_KEY")) | |
| chat_completion = client.chat.completions.create( | |
| messages=[{"role": "user", "content": prompt}], | |
| model="gpt-4o-mini", | |
| max_tokens=1024, | |
| ) | |
| st.markdown(chat_completion.choices[0].message.content) | |
| elif provider == "Anthropic": | |
| client = Anthropic(api_key=os.getenv("ANTHROPIC_API_KEY")) | |
| message = client.messages.create( | |
| messages=[{"role": "user", "content": prompt}], | |
| model="claude-3-haiku-20240307", | |
| max_tokens=1024, | |
| ) | |
| st.markdown(message.content[0].text) | |
| elif provider == "Gemini": | |
| client = genai.Client(api_key=os.environ["GOOGLE_API_KEY"]) | |
| response = client.models.generate_content( | |
| model='gemini-2.5-flash-lite', | |
| contents = types.Content( | |
| role='user', | |
| parts=[types.Part.from_text(text=prompt)] | |
| ), | |
| ) | |
| st.markdown(response.candidates[0].content.parts[0].text) | |