acecalisto3 commited on
Commit
c150b51
Β·
verified Β·
1 Parent(s): 80b94b9

Update appaaa.py

Browse files
Files changed (1) hide show
  1. appaaa.py +241 -273
appaaa.py CHANGED
@@ -1,290 +1,258 @@
1
- from fastapi import FastAPI, File, UploadFile, HTTPException, Form
2
- from fastapi.responses import FileResponse
3
- from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
4
- from zipfile import ZipFile, BadZipfile
5
- import os
6
- import shutil
7
- import subprocess
8
  from datetime import datetime
9
- import logging
10
- import asyncio
11
- from typing import List
12
- from fastapi.middleware.cors import CORSMiddleware
13
- from pydantic import BaseModel
14
- from fastapi.staticfiles import StaticFiles
15
- from fastapi.templating import Jinja2Templates
16
- from fastapi.responses import HTMLResponse
17
- from fastapi.responses import RedirectResponse
18
- from sqlalchemy import create_engine
19
- from sqlalchemy.ext.declarative import declarative_base
20
- from sqlalchemy.orm import sessionmaker
21
- from sqlalchemy import Column, Integer, String, DateTime, Boolean
22
- from langchain.document_loaders import TextLoader
23
- from langchain.embeddings import HuggingFaceEmbeddings
24
- from langchain.vectorstores import FAISS
25
-
26
 
27
- def initialize_rag_system():
28
- # Document processing
29
- loader = TextLoader("deepseek_rag/documents/*.txt")
30
- documents = loader.load()
 
 
 
 
31
 
32
- # Text splitting
33
- text_splitter = RecursiveCharacterTextSplitter(
34
- chunk_size=1000,
35
- chunk_overlap=200
36
- )
37
 
38
- # Vector storage
39
- embeddings = HuggingFaceEmbeddings()
40
- vector_store = FAISS.from_documents(
41
- text_splitter.split_documents(documents),
42
- embeddings
43
- )
 
 
44
 
45
- return vector_store.as_retriever()
46
-
47
- # Set up logging
48
- logging.basicConfig(filename="errors.log", level=logging.ERROR, format="%(asctime)s - %(levelname)s - %(message)s")
49
-
50
- app = FastAPI()
51
-
52
- # Enable CORS for development
53
- origins = ["*"]
54
- app.add_middleware(
55
- CORSMiddleware,
56
- allow_origins=origins,
57
- allow_credentials=True,
58
- allow_methods=["*"],
59
- allow_headers=["*"],
60
- )
61
-
62
- # Serve static files
63
- app.mount("/static", StaticFiles(directory="static"), name="static")
64
-
65
- # Template engine
66
- templates = Jinja2Templates(directory="templates")
67
-
68
- # Define supported frameworks and their associated extensions
69
- FRAMEWORKS = {
70
- "Python": ".py",
71
- "JavaScript": ".js",
72
- "PHP": ".php",
73
- "Go": ".go",
74
- "Ruby": ".rb",
75
- "Java": ".java",
76
- "C#": ".cs",
77
- "C++": ".cpp",
78
- "Rust": ".rs",
79
- }
80
-
81
- INTERPRETER_COMMANDS = {
82
- "Python": "python {file_path}",
83
- "JavaScript": "node {file_path}",
84
- "PHP": "php -l {file_path}",
85
- "Go": "go build {file_path}",
86
- "Ruby": "ruby -c {file_path}",
87
- "Java": "javac {file_path}",
88
- "C#": "mcs {file_path}",
89
- "C++": "g++ -c {file_path}",
90
- "Rust": "rustc {file_path}",
91
- }
92
-
93
- # Database setup
94
- SQLALCHEMY_DATABASE_URL = "sqlite:///errors.db" # Use SQLite for simplicity
95
- engine = create_engine(
96
- SQLALCHEMY_DATABASE_URL, connect_args={"check_same_thread": False}
97
- )
98
- SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
99
- Base = declarative_base()
100
-
101
- # Database model for errors
102
- class TranslationError(Base):
103
- __tablename__ = "translation_errors"
104
-
105
- id = Column(Integer, primary_key=True, index=True)
106
- timestamp = Column(DateTime, default=datetime.utcnow)
107
- file = Column(String)
108
- error = Column(String)
109
- resolved = Column(Boolean, default=False)
110
-
111
- # Create tables if they don't exist
112
- Base.metadata.create_all(bind=engine)
113
-
114
- # Dependency for database session
115
- def get_db():
116
- db = SessionLocal()
117
- try:
118
- yield db
119
- finally:
120
- db.close()
121
-
122
- # Load models at startup
123
- @app.on_event("startup")
124
- async def load_models():
125
- app.state.translation_models = {
126
- "Python": AutoModelForSeq2SeqLM.from_pretrained("microsoft/codebert-base"),
127
- "JavaScript": AutoModelForSeq2SeqLM.from_pretrained("microsoft/codebert-base"),
128
- # Add other frameworks as needed
129
  }
130
- app.state.tokenizer = AutoTokenizer.from_pretrained("microsoft/codebert-base")
131
-
132
- # Function to translate code using the loaded model
133
- def translate_code(input_code, framework):
134
- prompt = f"Translate TSX to {framework}:\n{input_code}"
135
- inputs = app.state.tokenizer(prompt, return_tensors="pt", truncation=True)
136
- outputs = app.state.translation_models[framework].generate(**inputs)
137
- return app.state.tokenizer.decode(outputs[0], skip_special_tokens=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
138
 
139
- # Function to process the uploaded zip file and translate its contents
140
- def process_zip(zip_path, framework):
141
- translated_dir = os.path.join(os.getcwd(), "translated")
142
- os.makedirs(translated_dir, exist_ok=True)
 
 
 
 
143
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
144
  try:
145
- with ZipFile(zip_path, 'r') as zip_ref:
146
- zip_ref.extractall(translated_dir)
147
- except BadZipfile:
148
- logging.error(f"Invalid ZIP file: {zip_path}")
149
- raise HTTPException(status_code=400, detail="Invalid ZIP file.")
150
-
151
- for root, _, files in os.walk(translated_dir):
152
- for file in files:
153
- if file.endswith(".tsx"):
154
- tsx_path = os.path.join(root, file)
155
- try:
156
- translated_code = translate_code(open(tsx_path).read(), framework)
157
- new_path = tsx_path.replace(".tsx", FRAMEWORKS[framework])
158
- with open(new_path, 'w') as f:
159
- f.write(translated_code)
160
- os.remove(tsx_path)
161
- except Exception as e:
162
- logging.error(f"Error translating {file}: {e}")
163
- raise HTTPException(status_code=500, detail="Translation failed.")
164
-
165
- output_zip = os.path.join(os.getcwd(), "translated_app.zip")
166
- with ZipFile(output_zip, 'w') as zipf:
167
- for foldername, _, filenames in os.walk(translated_dir):
168
- for filename in filenames:
169
- zipf.write(os.path.join(foldername, filename), arcname=filename)
170
- return output_zip
171
-
172
- # Chat endpoint to handle user queries
173
- @app.post("/chat")
174
- async def handle_chat(query: dict):
175
- error_context = get_last_translation_errors() # Retrieve from DB/state
176
- response = generate_ai_response(query['message'], error_context)
177
- return {"response": response}
178
-
179
- def generate_ai_response(message, errors=None):
180
- if "error" in message.lower() and errors:
181
- return """f"Your translation failed with: {', '.join(errors[:3])}"""
182
- elif "support" in message.lower():
183
- return """f"Supported frameworks: {', '.join(FRAMEWORKS.keys())}"""
184
- return "Ask about translation errors or framework support"
185
 
186
- # Translation endpoint to handle file uploads and translations
187
- @app.post("/translate")
188
- async def translate(files: List[UploadFile] = File(...), framework: str = Form(...), retries: int = 3):
189
- if framework not in FRAMEWORKS:
190
- logging.error(f"Unsupported framework: {framework}")
191
- raise HTTPException(status_code=400, detail="Unsupported framework.")
192
 
193
- for file in files:
194
- file_path = os.path.join(os.getcwd(), file.filename)
195
- with open(file_path, "wb") as buffer:
196
- shutil.copyfileobj(file.file, buffer)
197
 
198
- for attempt in range(retries):
 
 
 
 
 
 
199
  try:
200
- translated_zip = process_zip(file_path, framework)
201
- return FileResponse(translated_zip)
 
 
 
 
 
 
 
 
 
202
  except Exception as e:
203
- logging.error(f"Translation attempt {attempt + 1} failed: {e}")
204
- if attempt < retries - 1:
205
- # Exponential backoff (adjust as needed)
206
- wait_time = 2 ** attempt
207
- print(f"Retrying translation in {wait_time} seconds...")
208
- await asyncio.sleep(wait_time)
209
- else:
210
- return {"error": "Translation failed after multiple attempts."}
211
-
212
- # Error detection function
213
- def pre_execute_error_detection(file_path, framework):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
214
  try:
215
- result = subprocess.run(
216
- INTERPRETER_COMMANDS[framework].format(file_path=file_path),
217
- capture_output=True,
218
- text=True,
219
- shell=True
220
- )
221
- if result.returncode != 0:
222
- log_error(file_path, result.stderr) # Store in persistent storage
223
- return False, result.stderr
224
- return True, ""
225
  except Exception as e:
226
- return False, str(e)
227
-
228
- # Error logging function
229
- def log_error(file_path, error, db: SessionLocal):
230
- error_entry = TranslationError(file=file_path, error=error)
231
- db.add(error_entry)
232
- db.commit()
233
-
234
- # Function to retrieve last translation errors
235
- def get_last_translation_errors(db: SessionLocal):
236
- errors = db.query(TranslationError).order_by(TranslationError.timestamp.desc()).limit(5).all()
237
- return [error.error for error in errors]
238
-
239
- # Chat input model
240
- class ChatInput(BaseModel):
241
- message: str
242
-
243
- # Chat endpoint with HTML template
244
- @app.get("/chat")
245
- async def chat_page(request: ChatInput, db: SessionLocal = Depends(get_db)):
246
- error_context = get_last_translation_errors(db)
247
- response = generate_ai_response(request.message, error_context)
248
- return templates.TemplateResponse("chat.html", {"request": request, "response": response})
249
-
250
- # Home page
251
- @app.get("/")
252
- async def home_page(request: ChatInput):
253
- return templates.TemplateResponse("index.html", {"request": request})
254
-
255
- # Translation page
256
- @app.get("/translate")
257
- async def translate_page(request: ChatInput):
258
- return templates.TemplateResponse("translate.html", {"request": request})
259
-
260
- # Handle translation form submission
261
- @app.post("/translate")
262
- async def handle_translate(files: List[UploadFile] = File(...), framework: str = Form(...), retries: int = 3, db: SessionLocal = Depends(get_db)):
263
- if framework not in FRAMEWORKS:
264
- logging.error(f"Unsupported framework: {framework}")
265
- raise HTTPException(status_code=400, detail="Unsupported framework.")
266
-
267
- for file in files:
268
- file_path = os.path.join(os.getcwd(), file.filename)
269
- with open(file_path, "wb") as buffer:
270
- shutil.copyfileobj(file.file, buffer)
271
-
272
- for attempt in range(retries):
273
- try:
274
- translated_zip = process_zip(file_path, framework)
275
- return FileResponse(translated_zip)
276
- except Exception as e:
277
- logging.error(f"Translation attempt {attempt + 1} failed: {e}")
278
- log_error(file_path, str(e), db)
279
- if attempt < retries - 1:
280
- # Exponential backoff (adjust as needed)
281
- wait_time = 2 ** attempt
282
- print(f"Retrying translation in {wait_time} seconds...")
283
- await asyncio.sleep(wait_time)
284
- else:
285
- return {"error": "Translation failed after multiple attempts."}
286
 
287
- # Run the application
288
- if __name__ == "__main__":
289
- import uvicorn
290
- uvicorn.run(app, host="0.0.0.0", port=8000)
 
1
+ import streamlit as st
 
 
 
 
 
 
2
  from datetime import datetime
3
+ import os
4
+ import json
5
+ from pathlib import Path
6
+ from json import JSONDecodeError
7
+ from FreeLLM import HuggingChatAPI, ChatGPTAPI, BingChatAPI
8
+
9
+ # Page Configuration
10
+ st.set_page_config(
11
+ page_title="FREE AUTOHUGGS πŸ€—",
12
+ page_icon="πŸ€—",
13
+ layout="wide",
14
+ menu_items={
15
+ "Get help": "[email protected]",
16
+ "About": "# *πŸ€— FREE AUTOHUGGS πŸ€—*"
17
+ }
18
+ )
 
19
 
20
+ # Modern Dark Theme CSS
21
+ st.markdown("""
22
+ <style>
23
+ /* Base theme */
24
+ .main {
25
+ background-color: #0e1117;
26
+ color: #e0e0e0;
27
+ }
28
 
29
+ /* Sidebar styling */
30
+ .css-1d391kg {
31
+ background-color: #1e1e2e;
32
+ }
 
33
 
34
+ /* Input fields */
35
+ .stTextInput input, .stTextArea textarea, .stNumberInput input {
36
+ background-color: #262630;
37
+ color: #ffffff;
38
+ border: 1px solid #464660;
39
+ border-radius: 8px;
40
+ padding: 12px;
41
+ }
42
 
43
+ /* Buttons */
44
+ .stButton>button {
45
+ background: linear-gradient(90deg, #ff4b4b 0%, #ff6b6b 100%);
46
+ color: white;
47
+ border: none;
48
+ padding: 0.6em 1.2em;
49
+ border-radius: 8px;
50
+ font-weight: 600;
51
+ transition: all 0.3s ease;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
52
  }
53
+
54
+ .stButton>button:hover {
55
+ transform: translateY(-2px);
56
+ box-shadow: 0 4px 12px rgba(255, 75, 75, 0.3);
57
+ }
58
+
59
+ /* Chat messages */
60
+ .chat-message {
61
+ padding: 1rem;
62
+ border-radius: 12px;
63
+ margin: 1rem 0;
64
+ box-shadow: 0 2px 8px rgba(0,0,0,0.1);
65
+ }
66
+
67
+ .user-message {
68
+ background-color: #2a2a3a;
69
+ border-left: 4px solid #ff4b4b;
70
+ }
71
+
72
+ .assistant-message {
73
+ background-color: #323242;
74
+ border-left: 4px solid #4b8bff;
75
+ }
76
+
77
+ /* Headers */
78
+ h1, h2, h3 {
79
+ color: #ffffff;
80
+ }
81
+ </style>
82
+ """, unsafe_allow_html=True)
83
 
84
+ # Sidebar Configuration
85
+ with st.sidebar:
86
+ st.header("πŸ› οΈ Agent Configuration")
87
+
88
+ assistant_role_name = st.text_input("πŸ‘€ Assistant Role", "Python Programmer")
89
+ user_role_name = st.text_input("πŸ‘₯ User Role", "Stock Trader")
90
+ task = st.text_area("πŸ“‹ Task", "Develop a trading bot for the stock market")
91
+ word_limit = st.slider("πŸ”€ Word Limit", 10, 1500, 50)
92
 
93
+ st.markdown("---")
94
+
95
+ st.header("βš™οΈ Advanced Options")
96
+ llm_choice = st.selectbox("πŸ€– Select LLM API", ["HuggingChat", "ChatGPT", "BingChat"])
97
+ auto_save = st.checkbox("πŸ’Ύ Auto-save conversation", value=True)
98
+ transcript_filename = st.text_input("πŸ“„ Transcript filename", "conversation_transcript.txt")
99
+
100
+ # Cookie File Validation
101
+ COOKIE_FILE = "cookiesHuggingChat.json"
102
+ try:
103
+ if not Path(COOKIE_FILE).exists():
104
+ st.error(f"❌ File '{COOKIE_FILE}' not found! Please create it with your cookies in JSON format.")
105
+ st.stop()
106
+
107
+ with open(COOKIE_FILE, "r") as file:
108
+ cookies = json.loads(file.read())
109
+ except JSONDecodeError:
110
+ st.error(f"❌ Invalid JSON in '{COOKIE_FILE}'. Please check your file format!")
111
+ st.stop()
112
+ except Exception as e:
113
+ st.error(f"❌ Error reading cookie file: {str(e)}")
114
+ st.stop()
115
+
116
+ # LLM API Initialization
117
+ def get_llm(api_choice: str):
118
+ llm_map = {
119
+ "HuggingChat": HuggingChatAPI.HuggingChat,
120
+ "ChatGPT": ChatGPTAPI.ChatGPT,
121
+ "BingChat": BingChatAPI.BingChat
122
+ }
123
+
124
+ if api_choice not in llm_map:
125
+ st.error("❌ Invalid LLM API selection.")
126
+ st.stop()
127
+
128
  try:
129
+ return llm_map[api_choice](cookiepath=str(Path(COOKIE_FILE)))
130
+ except Exception as e:
131
+ st.error(f"❌ Error initializing {api_choice}: {str(e)}")
132
+ st.stop()
133
+
134
+ llm = get_llm(llm_choice)
135
+
136
+ # Initialize session state
137
+ if "chat_history" not in st.session_state:
138
+ st.session_state.chat_history = []
139
+ if "conversation_start" not in st.session_state:
140
+ st.session_state.conversation_start = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
141
+
142
+ # Main chat interface
143
+ st.title("πŸ’¬ AI Chat Interface")
144
+
145
+ # Display chat history
146
+ for message in st.session_state.chat_history:
147
+ if message["role"] == "assistant":
148
+ st.markdown(
149
+ f"<div class='chat-message assistant-message'><strong>{assistant_role_name}:</strong> {message['content']}</div>",
150
+ unsafe_allow_html=True
151
+ )
152
+ elif message["role"] == "user":
153
+ st.markdown(
154
+ f"<div class='chat-message user-message'><strong>{user_role_name}:</strong> {message['content']}</div>",
155
+ unsafe_allow_html=True
156
+ )
157
+ else:
158
+ st.markdown(
159
+ f"<div class='chat-message'><em>{message['content']}</em></div>",
160
+ unsafe_allow_html=True
161
+ )
 
 
 
 
 
 
 
162
 
163
+ # Input area
164
+ user_input = st.text_input("πŸ’­ Your message:", key="user_input")
 
 
 
 
165
 
166
+ col1, col2, col3 = st.columns(3)
 
 
 
167
 
168
+ with col1:
169
+ if st.button("πŸ“€ Send"):
170
+ if user_input:
171
+ # Add user message to history
172
+ st.session_state.chat_history.append({"role": "user", "content": user_input})
173
+
174
+ # Get AI response
175
  try:
176
+ with st.spinner("πŸ€” AI is thinking..."):
177
+ response = llm(user_input)
178
+ st.session_state.chat_history.append({"role": "assistant", "content": response})
179
+
180
+ # Auto-save if enabled
181
+ if auto_save:
182
+ with open(transcript_filename, "a", encoding="utf-8") as f:
183
+ f.write(f":User {user_input}\n")
184
+ f.write(f"Assistant: {response}\n")
185
+
186
+ st.experimental_rerun()
187
  except Exception as e:
188
+ st.error(f"❌ Error getting AI response: {str(e)}")
189
+ with col2:
190
+ if st.button("πŸ“ Clear Chat"):
191
+ st.session_state.chat_history = []
192
+ st.experimental_rerun()
193
+ with col3:
194
+ if st.button("πŸ’Ύ Save Transcript"):
195
+ try:
196
+ transcript = "\n".join([
197
+ f"{msg['role'].capitalize()}: {msg['content']}"
198
+ for msg in st.session_state.chat_history
199
+ ])
200
+ with open(transcript_filename, "w", encoding="utf-8") as f:
201
+ f.write(transcript)
202
+ st.success("βœ… Transcript saved successfully!")
203
+ except Exception as e:
204
+ st.error(f"❌ Error saving transcript: {str(e)}") ```python
205
+ # Additional Features
206
+ # Function to display the current time
207
+ def display_time():
208
+ current_time = datetime.now().strftime("%H:%M:%S")
209
+ st.sidebar.markdown(f"πŸ•’ Current Time: {current_time}")
210
+
211
+ # Call the function to display time
212
+ display_time()
213
+
214
+ # Function to handle user input and AI response
215
+ def handle_input(user_input):
216
+ if user_input:
217
+ st.session_state.chat_history.append({"role": "user", "content": user_input})
218
+ try:
219
+ with st.spinner("πŸ€” AI is thinking..."):
220
+ response = llm(user_input)
221
+ st.session_state.chat_history.append({"role": "assistant", "content": response})
222
+ if auto_save:
223
+ with open(transcript_filename, "a", encoding="utf-8") as f:
224
+ f.write(f":User {user_input}\n")
225
+ f.write(f"Assistant: {response}\n")
226
+ st.experimental_rerun()
227
+ except Exception as e:
228
+ st.error(f"❌ Error getting AI response: {str(e)}")
229
+
230
+ # Input area with button to send message
231
+ if st.button("πŸ“€ Send"):
232
+ handle_input(user_input)
233
+
234
+ # Function to clear chat history
235
+ def clear_chat():
236
+ st.session_state.chat_history = []
237
+ st.experimental_rerun()
238
+
239
+ # Clear chat button
240
+ if st.button("πŸ“ Clear Chat"):
241
+ clear_chat()
242
+
243
+ # Function to save transcript
244
+ def save_transcript():
245
  try:
246
+ transcript = "\n".join([
247
+ f"{msg['role'].capitalize()}: {msg['content']}"
248
+ for msg in st.session_state.chat_history
249
+ ])
250
+ with open(transcript_filename, "w", encoding="utf-8") as f:
251
+ f.write(transcript)
252
+ st.success("βœ… Transcript saved successfully!")
 
 
 
253
  except Exception as e:
254
+ st.error(f"❌ Error saving transcript: {str(e)}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
255
 
256
+ # Save transcript button
257
+ if st.button("πŸ’Ύ Save Transcript"):
258
+ save_transcript()